Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix scale “display” percent #651

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion ucrop/src/main/java/com/yalantis/ucrop/UCropActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,10 @@ private void setAngleTextColor(int textColor) {

private void setScaleText(float scale) {
if (mTextViewScalePercent != null) {
mTextViewScalePercent.setText(String.format(Locale.getDefault(), "%d%%", (int) (scale * 100)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ashiagr , I would suggest using

            String.format(Locale.getDefault(), "%d%%", Math.round(scale/ initialScale * 100))

This will prevent incorrect value like 99.99% when it should be 100%. Also integer value should be enough to display. Fractional part is redundant.

float initialScale = mGestureCropImageView.getInitialMinScale();
mTextViewScalePercent.setText(
String.format(Locale.getDefault(), "%.2f%%", (scale/ initialScale * 100))
);
}
}

Expand Down
5 changes: 4 additions & 1 deletion ucrop/src/main/java/com/yalantis/ucrop/UCropFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,10 @@ private void setAngleTextColor(int textColor) {

private void setScaleText(float scale) {
if (mTextViewScalePercent != null) {
mTextViewScalePercent.setText(String.format(Locale.getDefault(), "%d%%", (int) (scale * 100)));
float initialScale = mGestureCropImageView.getInitialMinScale();
mTextViewScalePercent.setText(
String.format(Locale.getDefault(), "%.2f%%", (scale/ initialScale * 100))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ashiagr , I would suggest using

            String.format(Locale.getDefault(), "%d%%", Math.round(scale/ initialScale * 100))

This will prevent incorrect value like 99.99% when it should be 100%. Also integer value should be enough to display. Fractional part is redundant.

);
}
}

Expand Down
16 changes: 12 additions & 4 deletions ucrop/src/main/java/com/yalantis/ucrop/view/CropImageView.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public class CropImageView extends TransformImageView {
private Runnable mWrapCropBoundsRunnable, mZoomImageToPositionRunnable = null;

private float mMaxScale, mMinScale;
private float mInitialMinScale;
private int mMaxResultImageSizeX = 0, mMaxResultImageSizeY = 0;
private long mImageToWrapCropBoundsAnimDuration = DEFAULT_IMAGE_TO_CROP_BOUNDS_ANIM_DURATION;

Expand Down Expand Up @@ -109,6 +110,13 @@ public float getTargetAspectRatio() {
return mTargetAspectRatio;
}

/**
* @return - initial scale value for current image and crop ratio
*/
public float getInitialMinScale() {
return mInitialMinScale != 0 ? mInitialMinScale : 1;
}

/**
* Updates current crop rectangle with given. Also recalculates image properties and position
* to fit new crop rectangle.
Expand Down Expand Up @@ -485,13 +493,13 @@ private void setupInitialImagePosition(float drawableWidth, float drawableHeight
float widthScale = mCropRect.width() / drawableWidth;
float heightScale = mCropRect.height() / drawableHeight;

float initialMinScale = Math.max(widthScale, heightScale);
mInitialMinScale = Math.max(widthScale, heightScale);

float tw = (cropRectWidth - drawableWidth * initialMinScale) / 2.0f + mCropRect.left;
float th = (cropRectHeight - drawableHeight * initialMinScale) / 2.0f + mCropRect.top;
float tw = (cropRectWidth - drawableWidth * mInitialMinScale) / 2.0f + mCropRect.left;
float th = (cropRectHeight - drawableHeight * mInitialMinScale) / 2.0f + mCropRect.top;

mCurrentImageMatrix.reset();
mCurrentImageMatrix.postScale(initialMinScale, initialMinScale);
mCurrentImageMatrix.postScale(mInitialMinScale, mInitialMinScale);
mCurrentImageMatrix.postTranslate(tw, th);
setImageMatrix(mCurrentImageMatrix);
}
Expand Down