Skip to content

Commit

Permalink
fix aspect ratio bugs
Browse files Browse the repository at this point in the history
- change fallback for EXTRA_ASPECT_RATIO_X and EXTRA_ASPECT_RATIO_Y to "-1" as "0" has special meaning
- fix usage of useSourceImageAspectRatio (now properly hides mWrapperStateAspectRatio)
- fix setTargetAspectRatio at processOptions due to Float.NaN (allows custom aspect ratio definitions with default ratio set to x=CropImageView.DEFAULT_ASPECT_RATIO and y=CropImageView.DEFAULT_ASPECT_RATIO)
- fix sanity check at setAspectRatioOptions
  • Loading branch information
Johannes Puchberger authored and dmitriy1morozov committed Jan 25, 2022
1 parent 45fd588 commit a8aaa56
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -349,12 +349,14 @@ Tune everything (ノ◕ヮ◕)ノ*:・゚✧
options.setActiveControlsWidgetColor(ContextCompat.getColor(this, R.color.your_color_res));
// Aspect ratio options
options.setAspectRatioOptions(1,
options.setAspectRatioOptions(2,
new AspectRatio("WOW", 1, 2),
new AspectRatio("MUCH", 3, 4),
new AspectRatio("RATIO", CropImageView.DEFAULT_ASPECT_RATIO, CropImageView.DEFAULT_ASPECT_RATIO),
new AspectRatio("SO", 16, 9),
new AspectRatio("ASPECT", 1, 1));
options.withAspectRatio(CropImageView.DEFAULT_ASPECT_RATIO, CropImageView.DEFAULT_ASPECT_RATIO);
options.useSourceImageAspectRatio();
*/

Expand Down
4 changes: 2 additions & 2 deletions ucrop/src/main/java/com/yalantis/ucrop/UCrop.java
Original file line number Diff line number Diff line change
Expand Up @@ -517,9 +517,9 @@ public void setFreeStyleCropEnabled(boolean enabled) {
* @param aspectRatio - list of aspect ratio options that are available to user
*/
public void setAspectRatioOptions(int selectedByDefault, AspectRatio... aspectRatio) {
if (selectedByDefault > aspectRatio.length) {
if (selectedByDefault >= aspectRatio.length) {
throw new IllegalArgumentException(String.format(Locale.US,
"Index [selectedByDefault = %d] cannot be higher than aspect ratio options count [count = %d].",
"Index [selectedByDefault = %d] (0-based) cannot be higher or equal than aspect ratio options count [count = %d].",
selectedByDefault, aspectRatio.length));
}
mOptionBundle.putInt(EXTRA_ASPECT_RATIO_SELECTED_BY_DEFAULT, selectedByDefault);
Expand Down
13 changes: 7 additions & 6 deletions ucrop/src/main/java/com/yalantis/ucrop/UCropActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -256,20 +256,21 @@ private void processOptions(@NonNull Intent intent) {
mOverlayView.setCropGridStrokeWidth(intent.getIntExtra(UCrop.Options.EXTRA_CROP_GRID_STROKE_WIDTH, getResources().getDimensionPixelSize(R.dimen.ucrop_default_crop_grid_stoke_width)));

// Aspect ratio options
float aspectRatioX = intent.getFloatExtra(UCrop.EXTRA_ASPECT_RATIO_X, 0);
float aspectRatioY = intent.getFloatExtra(UCrop.EXTRA_ASPECT_RATIO_Y, 0);
float aspectRatioX = intent.getFloatExtra(UCrop.EXTRA_ASPECT_RATIO_X, -1);
float aspectRatioY = intent.getFloatExtra(UCrop.EXTRA_ASPECT_RATIO_Y, -1);

int aspectRationSelectedByDefault = intent.getIntExtra(UCrop.Options.EXTRA_ASPECT_RATIO_SELECTED_BY_DEFAULT, 0);
ArrayList<AspectRatio> aspectRatioList = intent.getParcelableArrayListExtra(UCrop.Options.EXTRA_ASPECT_RATIO_OPTIONS);

if (aspectRatioX > 0 && aspectRatioY > 0) {
if (aspectRatioX >= 0 && aspectRatioY >= 0) {
if (mWrapperStateAspectRatio != null) {
mWrapperStateAspectRatio.setVisibility(View.GONE);
}
mGestureCropImageView.setTargetAspectRatio(aspectRatioX / aspectRatioY);
float targetAspectRatio = aspectRatioX / aspectRatioY;
mGestureCropImageView.setTargetAspectRatio(Float.isNaN(targetAspectRatio) ? CropImageView.SOURCE_IMAGE_ASPECT_RATIO : targetAspectRatio);
} else if (aspectRatioList != null && aspectRationSelectedByDefault < aspectRatioList.size()) {
mGestureCropImageView.setTargetAspectRatio(aspectRatioList.get(aspectRationSelectedByDefault).getAspectRatioX() /
aspectRatioList.get(aspectRationSelectedByDefault).getAspectRatioY());
float targetAspectRatio = aspectRatioList.get(aspectRationSelectedByDefault).getAspectRatioX() / aspectRatioList.get(aspectRationSelectedByDefault).getAspectRatioY();
mGestureCropImageView.setTargetAspectRatio(Float.isNaN(targetAspectRatio) ? CropImageView.SOURCE_IMAGE_ASPECT_RATIO : targetAspectRatio);
} else {
mGestureCropImageView.setTargetAspectRatio(CropImageView.SOURCE_IMAGE_ASPECT_RATIO);
}
Expand Down
13 changes: 7 additions & 6 deletions ucrop/src/main/java/com/yalantis/ucrop/UCropFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -237,20 +237,21 @@ private void processOptions(@NonNull Bundle bundle) {
mOverlayView.setCropGridStrokeWidth(bundle.getInt(UCrop.Options.EXTRA_CROP_GRID_STROKE_WIDTH, getResources().getDimensionPixelSize(R.dimen.ucrop_default_crop_grid_stoke_width)));

// Aspect ratio options
float aspectRatioX = bundle.getFloat(UCrop.EXTRA_ASPECT_RATIO_X, 0);
float aspectRatioY = bundle.getFloat(UCrop.EXTRA_ASPECT_RATIO_Y, 0);
float aspectRatioX = bundle.getFloat(UCrop.EXTRA_ASPECT_RATIO_X, -1);
float aspectRatioY = bundle.getFloat(UCrop.EXTRA_ASPECT_RATIO_Y, -1);

int aspectRationSelectedByDefault = bundle.getInt(UCrop.Options.EXTRA_ASPECT_RATIO_SELECTED_BY_DEFAULT, 0);
ArrayList<AspectRatio> aspectRatioList = bundle.getParcelableArrayList(UCrop.Options.EXTRA_ASPECT_RATIO_OPTIONS);

if (aspectRatioX > 0 && aspectRatioY > 0) {
if (aspectRatioX >= 0 && aspectRatioY >= 0) {
if (mWrapperStateAspectRatio != null) {
mWrapperStateAspectRatio.setVisibility(View.GONE);
}
mGestureCropImageView.setTargetAspectRatio(aspectRatioX / aspectRatioY);
float targetAspectRatio = aspectRatioX / aspectRatioY;
mGestureCropImageView.setTargetAspectRatio(Float.isNaN(targetAspectRatio) ? CropImageView.SOURCE_IMAGE_ASPECT_RATIO : targetAspectRatio);
} else if (aspectRatioList != null && aspectRationSelectedByDefault < aspectRatioList.size()) {
mGestureCropImageView.setTargetAspectRatio(aspectRatioList.get(aspectRationSelectedByDefault).getAspectRatioX() /
aspectRatioList.get(aspectRationSelectedByDefault).getAspectRatioY());
float targetAspectRatio = aspectRatioList.get(aspectRationSelectedByDefault).getAspectRatioX() / aspectRatioList.get(aspectRationSelectedByDefault).getAspectRatioY();
mGestureCropImageView.setTargetAspectRatio(Float.isNaN(targetAspectRatio) ? CropImageView.SOURCE_IMAGE_ASPECT_RATIO : targetAspectRatio);
} else {
mGestureCropImageView.setTargetAspectRatio(CropImageView.SOURCE_IMAGE_ASPECT_RATIO);
}
Expand Down

0 comments on commit a8aaa56

Please sign in to comment.