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 CropTransformation for use case with width = 0 and height = 0 #201

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

public class CropTransformation extends BitmapTransformation {

private static final int VERSION = 1;
private static final int VERSION = 2;
private static final String ID = "jp.wasabeef.glide.transformations.CropTransformation." + VERSION;

public enum CropType {
Expand All @@ -38,10 +38,9 @@ public enum CropType {
BOTTOM
}

private int width;
private int height;

private CropType cropType = CropType.CENTER;
final private int width;
final private int height;
final private CropType cropType;

public CropTransformation(int width, int height) {
this(width, height, CropType.CENTER);
Expand All @@ -56,24 +55,25 @@ public CropTransformation(int width, int height, CropType cropType) {
@Override
protected Bitmap transform(@NonNull Context context, @NonNull BitmapPool pool,
@NonNull Bitmap toTransform, int outWidth, int outHeight) {
int toWidth, toHeight;

width = width == 0 ? toTransform.getWidth() : width;
height = height == 0 ? toTransform.getHeight() : height;
toWidth = width == 0 ? outWidth : width;
toHeight = height == 0 ? outHeight : height;

Bitmap.Config config =
toTransform.getConfig() != null ? toTransform.getConfig() : Bitmap.Config.ARGB_8888;
Bitmap bitmap = pool.get(width, height, config);

bitmap.setHasAlpha(true);

float scaleX = (float) width / toTransform.getWidth();
float scaleY = (float) height / toTransform.getHeight();
float scaleX = (float) toWidth / toTransform.getWidth();
float scaleY = (float) toHeight / toTransform.getHeight();
float scale = Math.max(scaleX, scaleY);

float scaledWidth = scale * toTransform.getWidth();
float scaledHeight = scale * toTransform.getHeight();
float left = (width - scaledWidth) / 2;
float top = getTop(scaledHeight);
float left = (toWidth - scaledWidth) / 2;
float top = getTop(toHeight, scaledHeight);
RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);

setCanvasBitmapDensity(toTransform, bitmap);
Expand All @@ -84,14 +84,14 @@ protected Bitmap transform(@NonNull Context context, @NonNull BitmapPool pool,
return bitmap;
}

private float getTop(float scaledHeight) {
private float getTop(float toHeight, float scaledHeight) {
switch (cropType) {
case TOP:
return 0;
case CENTER:
return (height - scaledHeight) / 2;
return (toHeight - scaledHeight) / 2;
case BOTTOM:
return height - scaledHeight;
return toHeight - scaledHeight;
default:
return 0;
}
Expand Down