From 927c8e3a035cae7ef6473b94ffcdbcc2dbf58285 Mon Sep 17 00:00:00 2001 From: taslimisina Date: Sat, 15 May 2021 23:06:23 +0430 Subject: [PATCH] Resolve igreenwood/SimpleCropView#29, Crop rectangle based on coordinates with calling setFrameRect function in CropImageView --- .../simplecropview/CropImageView.java | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/simplecropview/src/main/java/com/isseiaoki/simplecropview/CropImageView.java b/simplecropview/src/main/java/com/isseiaoki/simplecropview/CropImageView.java index b515aa7..412ff7e 100644 --- a/simplecropview/src/main/java/com/isseiaoki/simplecropview/CropImageView.java +++ b/simplecropview/src/main/java/com/isseiaoki/simplecropview/CropImageView.java @@ -1003,13 +1003,12 @@ private boolean isHeightTooSmall() { // Frame aspect ratio correction /////////////////////////////////////////////////////////////// - private void recalculateFrameRect(int durationMillis) { + private void recalculateFrameRect(int durationMillis, final RectF newRect) { if (mImageRect == null) return; if (mIsAnimating) { getAnimator().cancelAnimation(); } final RectF currentRect = new RectF(mFrameRect); - final RectF newRect = calcFrameRect(mImageRect); final float diffL = newRect.left - currentRect.left; final float diffT = newRect.top - currentRect.top; final float diffR = newRect.right - currentRect.right; @@ -1023,7 +1022,7 @@ private void recalculateFrameRect(int durationMillis) { @Override public void onAnimationUpdated(float scale) { mFrameRect = new RectF(currentRect.left + diffL * scale, currentRect.top + diffT * scale, - currentRect.right + diffR * scale, currentRect.bottom + diffB * scale); + currentRect.right + diffR * scale, currentRect.bottom + diffB * scale); invalidate(); } @@ -1035,11 +1034,15 @@ private void recalculateFrameRect(int durationMillis) { }); animator.startAnimation(durationMillis); } else { - mFrameRect = calcFrameRect(mImageRect); + mFrameRect = newRect; invalidate(); } } + private void recalculateFrameRect(int durationMillis) { + recalculateFrameRect(durationMillis, calcFrameRect(mImageRect)); + } + private float getRatioX(float w) { switch (mCropMode) { case FIT_IMAGE: @@ -1960,6 +1963,20 @@ public void setCropMode(CropMode mode, int durationMillis) { } } + /** + * Set frame rectangle + * + * @param newRect new frame rectangle relative to mImageRect + */ + public void setFrameRect(RectF newRect) { + mCropMode = CropMode.FREE; + RectF absoluteRect = new RectF( + newRect.left + mImageRect.left, newRect.top + mImageRect.top, + newRect.right + mImageRect.left, newRect.bottom + mImageRect.top + ); + recalculateFrameRect(mAnimationDurationMillis, absoluteRect); + } + /** * Set getCroppedBitmap mode *