ArthurHub / Android-Image-Cropper

Image Cropping Library for Android, optimized for Camera / Gallery.
Apache License 2.0
6.38k stars 1.34k forks source link

Multi-touch breaks Aspect Ratio #161

Open markxh opened 7 years ago

markxh commented 7 years ago

When multti touch is enabled to pinch-zoom, the Aspect Ratio does not persist.

VasiliyMikhailov commented 7 years ago

I am waiting for this fix too.

klob commented 7 years ago

maybe help ?

     /**
     * Handle scaling the rectangle based on two finger input
     */
    private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {

        @Override
        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        public boolean onScale(ScaleGestureDetector detector) {
            RectF rect = mCropWindowHandler.getRect();

            float scaleFactor = detector.getScaleFactor();
            scaleFactor = Math.max(0.5f, Math.min(scaleFactor, 1.5f));

            float width = rect.width();
            float height = rect.height();

            float newWidth = rect.width() * scaleFactor;
            float newHeight = rect.height() * scaleFactor;

            float originCenterX = (rect.left + rect.right) / 2;
            float originCenterY = (rect.top + rect.bottom) / 2;

            float x = detector.getFocusX();
            float y = detector.getFocusY();
            float dY = detector.getCurrentSpanY() / 2;
            float dX = detector.getCurrentSpanX() / 2;

            float newTop = originCenterY - newHeight / 2;
            float newLeft = originCenterX - newWidth / 2;
            float newRight = originCenterX + newWidth / 2;
            float newBottom = originCenterY + newHeight / 2;

            if (newLeft < newRight &&
                    newTop <= newBottom &&
                    newLeft >= 0 &&
                    newWidth <= mCropWindowHandler.getMaxCropWidth() &&
                    newTop >= 0 &&
                    newHeight <= mCropWindowHandler.getMaxCropHeight()) {

                rect.set(newLeft, newTop, newRight, newBottom);
                mCropWindowHandler.setRect(rect);
                invalidate();
            }

            return true;
        }
    }
ben-j69 commented 7 years ago

I have the same issue.

francescogatto commented 7 years ago

Same for me

xraylee commented 7 years ago

@klob This is my solution:

/**
 * Handle scaling the rectangle based on two finger input
 */
private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {

    @Override
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public boolean onScale(ScaleGestureDetector detector) {
        RectF rect = mCropWindowHandler.getRect();

        float scaleFactor = detector.getScaleFactor();
        scaleFactor = Math.max(0.5f, Math.min(scaleFactor, 1.5f));

        float width = rect.width();
        float height = rect.height();

        float newWidth = rect.width() * scaleFactor;
        float newHeight = rect.height() * scaleFactor;

        float originCenterX = (rect.left + rect.right) / 2;
        float originCenterY = (rect.top + rect.bottom) / 2;

        float x = detector.getFocusX();
        float y = detector.getFocusY();
        float dY = detector.getCurrentSpanY() / 2;
        float dX = detector.getCurrentSpanX() / 2;

        float bitmapTop = Math.max(0, BitmapUtils.getRectTop(mBoundsPoints)); //top border
        float bitmapLeft = Math.max(0, BitmapUtils.getRectLeft(mBoundsPoints)); //left border
        float bitmapRight = Math.min(getWidth(), BitmapUtils.getRectRight(mBoundsPoints)); //right border
        float bitmapBottom = Math.min(getHeight(), BitmapUtils.getRectBottom(mBoundsPoints)); //bottom border

        float newTop = Math.max(bitmapTop, originCenterY - newHeight / 2);
        float newLeft = Math.max(bitmapLeft, originCenterX - newWidth / 2);
        float newRight = Math.min(bitmapRight, originCenterX + newWidth / 2);
        float newBottom = Math.min(bitmapBottom, originCenterY + newHeight / 2);

        newWidth = newRight - newLeft;
        newHeight = newBottom - newTop;

        if (newLeft < newRight &&
                newTop < newBottom &&
                newWidth <= mCropWindowHandler.getMaxCropWidth() &&
                newWidth >= mCropWindowHandler.getMinCropWidth() &&
                newHeight <= mCropWindowHandler.getMaxCropHeight() &&
                newHeight >= mCropWindowHandler.getMinCropHeight()) {
            rect.set(newLeft, newTop, newRight, newBottom);
            mCropWindowHandler.setRect(rect);
            invalidate();
        }

        return true;
    }
}
johnernest02 commented 7 years ago

Any updates here?