Skip to content

Commit

Permalink
apply aspect ratio range
Browse files Browse the repository at this point in the history
  • Loading branch information
mushroom0210 committed Jun 15, 2022
1 parent 63dda2e commit 2201a66
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
10 changes: 10 additions & 0 deletions ucrop/src/main/java/com/yalantis/ucrop/UCrop.java
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ public static class Options {

public static final String EXTRA_UCROP_ROOT_VIEW_BACKGROUND_COLOR = EXTRA_PREFIX + ".UcropRootViewBackgroundColor";

public static final String EXTRA_MIN_ASPECT_RATIO = EXTRA_PREFIX + ".MinAspectRatio";
public static final String EXTRA_MAX_ASPECT_RATIO = EXTRA_PREFIX + ".MaxAspectRatio";

private final Bundle mOptionBundle;

Expand Down Expand Up @@ -510,6 +512,14 @@ public void setFreeStyleCropEnabled(boolean enabled) {
mOptionBundle.putBoolean(EXTRA_FREE_STYLE_CROP, enabled);
}

public void setMaxAspectRatio(float x, float y) {
mOptionBundle.putFloat(EXTRA_MAX_ASPECT_RATIO, x / y);
}

public void setMinAspectRatio(float x, float y) {
mOptionBundle.putFloat(EXTRA_MIN_ASPECT_RATIO, x / y);
}

/**
* Pass an ordered list of desired aspect ratios that should be available for a user.
*
Expand Down
9 changes: 9 additions & 0 deletions ucrop/src/main/java/com/yalantis/ucrop/UCropActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,15 @@ private void processOptions(@NonNull Intent intent) {
// Overlay view options
mOverlayView.setFreestyleCropEnabled(intent.getBooleanExtra(UCrop.Options.EXTRA_FREE_STYLE_CROP, OverlayView.DEFAULT_FREESTYLE_CROP_MODE != OverlayView.FREESTYLE_CROP_MODE_DISABLE));

float minRatio = intent.getFloatExtra(UCrop.Options.EXTRA_MIN_ASPECT_RATIO, 0f);
float maxRatio = intent.getFloatExtra(UCrop.Options.EXTRA_MAX_ASPECT_RATIO, 0f);
if (minRatio > 0f) {
mOverlayView.setCropRectMinRatio(minRatio);
}
if (maxRatio > 0f) {
mOverlayView.setCropRectMaxRatio(maxRatio);
}

mOverlayView.setDimmedColor(intent.getIntExtra(UCrop.Options.EXTRA_DIMMED_LAYER_COLOR, getResources().getColor(R.color.ucrop_color_default_dimmed)));
mOverlayView.setCircleDimmedLayer(intent.getBooleanExtra(UCrop.Options.EXTRA_CIRCLE_DIMMED_LAYER, OverlayView.DEFAULT_CIRCLE_DIMMED_LAYER));

Expand Down
9 changes: 9 additions & 0 deletions ucrop/src/main/java/com/yalantis/ucrop/UCropFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,15 @@ private void processOptions(@NonNull Bundle bundle) {
// Overlay view options
mOverlayView.setFreestyleCropEnabled(bundle.getBoolean(UCrop.Options.EXTRA_FREE_STYLE_CROP, OverlayView.DEFAULT_FREESTYLE_CROP_MODE != OverlayView.FREESTYLE_CROP_MODE_DISABLE));

float minRatio = bundle.getFloat(UCrop.Options.EXTRA_MIN_ASPECT_RATIO, 0f);
float maxRatio = bundle.getFloat(UCrop.Options.EXTRA_MAX_ASPECT_RATIO, 0f);
if (minRatio > 0f) {
mOverlayView.setCropRectMinRatio(minRatio);
}
if (maxRatio > 0f) {
mOverlayView.setCropRectMaxRatio(maxRatio);
}

mOverlayView.setDimmedColor(bundle.getInt(UCrop.Options.EXTRA_DIMMED_LAYER_COLOR, getResources().getColor(R.color.ucrop_color_default_dimmed)));
mOverlayView.setCircleDimmedLayer(bundle.getBoolean(UCrop.Options.EXTRA_CIRCLE_DIMMED_LAYER, OverlayView.DEFAULT_CIRCLE_DIMMED_LAYER));

Expand Down
16 changes: 14 additions & 2 deletions ucrop/src/main/java/com/yalantis/ucrop/view/OverlayView.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public class OverlayView extends View {
private int mTouchPointThreshold;
private int mCropRectMinSize;
private int mCropRectCornerTouchAreaLineLength;
private float mCropRectMinRatio, mCropRectMaxRatio;

private OverlayViewChangeListener mCallback;

Expand Down Expand Up @@ -131,6 +132,14 @@ public void setFreestyleCropMode(@FreestyleMode int mFreestyleCropMode) {
postInvalidate();
}

public void setCropRectMinRatio(float minRatio) {
this.mCropRectMinRatio = minRatio;
}

public void setCropRectMaxRatio(float maxRatio) {
this.mCropRectMaxRatio = maxRatio;
}

/**
* Setter for {@link #mCircleDimmedLayer} variable.
*
Expand Down Expand Up @@ -390,8 +399,11 @@ && mTempRect.right < getRight() && mTempRect.bottom < getBottom()) {
return;
}

boolean changeHeight = mTempRect.height() >= mCropRectMinSize;
boolean changeWidth = mTempRect.width() >= mCropRectMinSize;
float ratio = mTempRect.width() / mTempRect.height();
boolean isWithinRange = ratio >= mCropRectMinRatio && ratio <= mCropRectMaxRatio;
boolean changeHeight = mTempRect.height() >= mCropRectMinSize && isWithinRange;
boolean changeWidth = mTempRect.width() >= mCropRectMinSize && isWithinRange;

mCropViewRect.set(
changeWidth ? mTempRect.left : mCropViewRect.left,
changeHeight ? mTempRect.top : mCropViewRect.top,
Expand Down

0 comments on commit 2201a66

Please sign in to comment.