From 8baab29ad4424224bb2873cd3e377ec1267884c9 Mon Sep 17 00:00:00 2001 From: Benny Chau Date: Tue, 22 Nov 2016 16:32:39 -0800 Subject: [PATCH] Expose image state. --- .../yalantis/ucrop/view/CropImageView.java | 43 +++++++++++-------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/ucrop/src/main/java/com/yalantis/ucrop/view/CropImageView.java b/ucrop/src/main/java/com/yalantis/ucrop/view/CropImageView.java index 9d6dbd411..3658feb73 100644 --- a/ucrop/src/main/java/com/yalantis/ucrop/view/CropImageView.java +++ b/ucrop/src/main/java/com/yalantis/ucrop/view/CropImageView.java @@ -73,9 +73,7 @@ public void cropAndSaveImage(@NonNull Bitmap.CompressFormat compressFormat, int cancelAllAnimations(); setImageToWrapCropBounds(false); - final ImageState imageState = new ImageState( - mCropRect, RectUtils.trapToRect(mCurrentImageCorners), - getCurrentScale(), getCurrentAngle()); + final ImageState imageState = getImageState(); final CropParameters cropParameters = new CropParameters( mMaxResultImageSizeX, mMaxResultImageSizeY, @@ -85,6 +83,13 @@ public void cropAndSaveImage(@NonNull Bitmap.CompressFormat compressFormat, int new BitmapCropTask(getViewBitmap(), imageState, cropParameters, cropCallback).execute(); } + /*** + * @return Return the current state of image include scale, angle, crop rect, and image rect. + */ + public ImageState getImageState() { + return new ImageState(mCropRect, RectUtils.trapToRect(mCurrentImageCorners), getCurrentScale(), getCurrentAngle()); + } + /** * @return - maximum scale value for current image and crop ratio */ @@ -106,20 +111,6 @@ public float getTargetAspectRatio() { return mTargetAspectRatio; } - /** - * Updates current crop rectangle with given. Also recalculates image properties and position - * to fit new crop rectangle. - * - * @param cropRect - new crop rectangle - */ - public void setCropRect(RectF cropRect) { - mTargetAspectRatio = cropRect.width() / cropRect.height(); - mCropRect.set(cropRect.left - getPaddingLeft(), cropRect.top - getPaddingTop(), - cropRect.right - getPaddingRight(), cropRect.bottom - getPaddingBottom()); - calculateImageScaleBounds(); - setImageToWrapCropBounds(); - } - /** * This method sets aspect ratio for crop bounds. * If {@link #SOURCE_IMAGE_ASPECT_RATIO} value is passed - aspect ratio is calculated @@ -145,6 +136,20 @@ public void setTargetAspectRatio(float targetAspectRatio) { } } + /** + * Updates current crop rectangle with given. Also recalculates image properties and position + * to fit new crop rectangle. + * + * @param cropRect - new crop rectangle + */ + public void setCropRect(RectF cropRect) { + mTargetAspectRatio = cropRect.width() / cropRect.height(); + mCropRect.set(cropRect.left - getPaddingLeft(), cropRect.top - getPaddingTop(), + cropRect.right - getPaddingRight(), cropRect.bottom - getPaddingBottom()); + calculateImageScaleBounds(); + setImageToWrapCropBounds(); + } + @Nullable public CropBoundsChangeListener getCropBoundsChangeListener() { return mCropBoundsChangeListener; @@ -303,7 +308,7 @@ public void setImageToWrapCropBounds(boolean animate) { final float[] currentImageSides = RectUtils.getRectSidesFromCorners(mCurrentImageCorners); deltaScale = Math.max(tempCropRect.width() / currentImageSides[0], - tempCropRect.height() / currentImageSides[1]); + tempCropRect.height() / currentImageSides[1]); deltaScale = deltaScale * currentScale - currentScale; } @@ -443,7 +448,7 @@ protected void zoomImageToPosition(float scale, float centerX, float centerY, lo final float deltaScale = scale - oldScale; post(mZoomImageToPositionRunnable = new ZoomImageToPosition(CropImageView.this, - durationMs, oldScale, deltaScale, centerX, centerY)); + durationMs, oldScale, deltaScale, centerX, centerY)); } private void calculateImageScaleBounds() {