Skip to content

Commit

Permalink
Obtain the correct size Bitmap in TransformationUtils.roundedCorners.
Browse files Browse the repository at this point in the history
Previously we were obtaining the requested image size. The requested
image size is ignored in roundedCorners because the transformation 
doesn’t scale. As a result we were weirdly stretching the image with
some combinations of image and requested dimensions.

Fixes #2472.
  • Loading branch information
sjudd committed Oct 12, 2017
1 parent c4db04e commit 0b5d1bc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.support.annotation.NonNull;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.util.Preconditions;
import com.bumptech.glide.util.Util;
import java.nio.ByteBuffer;
import java.security.MessageDigest;

Expand Down Expand Up @@ -51,18 +52,22 @@ public RoundedCorners(@SuppressWarnings("unused") Context context, int roundingR
@Override
protected Bitmap transform(
@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
return TransformationUtils.roundedCorners(pool, toTransform, outWidth, outHeight,
roundingRadius);
return TransformationUtils.roundedCorners(pool, toTransform, roundingRadius);
}

@Override
public boolean equals(Object o) {
return (o instanceof RoundedCorners) && ((RoundedCorners) o).roundingRadius == roundingRadius;
if (o instanceof RoundedCorners) {
RoundedCorners other = (RoundedCorners) o;
return roundingRadius == other.roundingRadius;
}
return false;
}

@Override
public int hashCode() {
return ID.hashCode() + roundingRadius;
return Util.hashCode(ID.hashCode(),
Util.hashCode(roundingRadius));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import android.os.Build;
import android.support.annotation.NonNull;
import android.util.Log;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.util.Preconditions;
import com.bumptech.glide.util.Synthetic;
Expand Down Expand Up @@ -397,16 +398,41 @@ private static Bitmap getAlphaSafeBitmap(@NonNull BitmapPool pool,
* @param roundingRadius the corner radius to be applied (in device-specific pixels).
* @return a {@link Bitmap} similar to inBitmap but with rounded corners.
* @throws IllegalArgumentException if roundingRadius, width or height is 0 or less.
*
* @deprecated Width and height are unused and ignored. Use
* {@link #roundedCorners(BitmapPool, Bitmap, int)} instead.
*/
@Deprecated
public static Bitmap roundedCorners(
@NonNull BitmapPool pool,
@NonNull Bitmap inBitmap,
@SuppressWarnings("unused") int width,
@SuppressWarnings("unused") int height,
int roundingRadius) {
return roundedCorners(pool, inBitmap, roundingRadius);
}

/**
* Creates a bitmap from a source bitmap and rounds the corners.
*
* <p>This method does <em>NOT</em> resize the given {@link Bitmap}, it only rounds it's corners.
* To both resize and round the corners of an image, consider
* {@link com.bumptech.glide.request.RequestOptions#transforms(Transformation[])} and/or
* {@link com.bumptech.glide.load.MultiTransformation}.
*
* @param inBitmap the source bitmap to use as a basis for the created bitmap.
* @param roundingRadius the corner radius to be applied (in device-specific pixels).
* @return a {@link Bitmap} similar to inBitmap but with rounded corners.
* @throws IllegalArgumentException if roundingRadius, width or height is 0 or less.
*/
public static Bitmap roundedCorners(@NonNull BitmapPool pool, @NonNull Bitmap inBitmap,
int width, int height, int roundingRadius) {
Preconditions.checkArgument(width > 0, "width must be greater than 0.");
Preconditions.checkArgument(height > 0, "height must be greater than 0.");
public static Bitmap roundedCorners(
@NonNull BitmapPool pool, @NonNull Bitmap inBitmap, int roundingRadius) {
Preconditions.checkArgument(roundingRadius > 0, "roundingRadius must be greater than 0.");

// Alpha is required for this transformation.
Bitmap toTransform = getAlphaSafeBitmap(pool, inBitmap);
Bitmap result = pool.get(width, height, Bitmap.Config.ARGB_8888);
Bitmap result =
pool.get(toTransform.getWidth(), toTransform.getHeight(), Bitmap.Config.ARGB_8888);

result.setHasAlpha(true);

Expand Down

0 comments on commit 0b5d1bc

Please sign in to comment.