Skip to content

Commit f36a9fa

Browse files
sjuddglide-copybara-robot
authored andcommittedAug 20, 2019
Add granular rounding for corners.
PiperOrigin-RevId: 264495417
1 parent 01addba commit f36a9fa

File tree

2 files changed

+140
-2
lines changed

2 files changed

+140
-2
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.bumptech.glide.load.resource.bitmap;
2+
3+
import android.graphics.Bitmap;
4+
import androidx.annotation.NonNull;
5+
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
6+
import com.bumptech.glide.util.Util;
7+
import java.nio.ByteBuffer;
8+
import java.security.MessageDigest;
9+
10+
/** A {@link BitmapTransformation} which has a different raddius for each corner of a bitmap. */
11+
public final class GranularRoundedCorners extends BitmapTransformation {
12+
private static final String ID = "com.bumptech.glide.load.resource.bitmap.GranularRoundedCorners";
13+
private static final byte[] ID_BYTES = ID.getBytes(CHARSET);
14+
15+
private final float topLeft;
16+
private final float topRight;
17+
private final float bottomRight;
18+
private final float bottomLeft;
19+
20+
/** Provide the radii to round the corners of the bitmap. */
21+
public GranularRoundedCorners(
22+
float topLeft, float topRight, float bottomRight, float bottomLeft) {
23+
this.topLeft = topLeft;
24+
this.topRight = topRight;
25+
this.bottomRight = bottomRight;
26+
this.bottomLeft = bottomLeft;
27+
}
28+
29+
@Override
30+
protected Bitmap transform(
31+
@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
32+
return TransformationUtils.roundedCorners(
33+
pool, toTransform, topLeft, topRight, bottomRight, bottomLeft);
34+
}
35+
36+
@Override
37+
public boolean equals(Object o) {
38+
if (o instanceof GranularRoundedCorners) {
39+
GranularRoundedCorners other = (GranularRoundedCorners) o;
40+
return topLeft == other.topLeft
41+
&& topRight == other.topRight
42+
&& bottomRight == other.bottomRight
43+
&& bottomLeft == other.bottomLeft;
44+
}
45+
return false;
46+
}
47+
48+
@Override
49+
public int hashCode() {
50+
int hashCode = Util.hashCode(ID.hashCode(), Util.hashCode(topLeft));
51+
hashCode = Util.hashCode(topRight, hashCode);
52+
hashCode = Util.hashCode(bottomRight, hashCode);
53+
return Util.hashCode(bottomLeft, hashCode);
54+
}
55+
56+
@Override
57+
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
58+
messageDigest.update(ID_BYTES);
59+
60+
byte[] radiusData =
61+
ByteBuffer.allocate(16)
62+
.putFloat(topLeft)
63+
.putFloat(topRight)
64+
.putFloat(bottomRight)
65+
.putFloat(bottomLeft)
66+
.array();
67+
messageDigest.update(radiusData);
68+
}
69+
}

‎library/src/main/java/com/bumptech/glide/load/resource/bitmap/TransformationUtils.java

+71-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import android.graphics.Color;
88
import android.graphics.Matrix;
99
import android.graphics.Paint;
10+
import android.graphics.Path;
1011
import android.graphics.PorterDuff;
1112
import android.graphics.PorterDuffXfermode;
1213
import android.graphics.RectF;
@@ -472,9 +473,71 @@ public static Bitmap roundedCorners(
472473
* @throws IllegalArgumentException if roundingRadius, width or height is 0 or less.
473474
*/
474475
public static Bitmap roundedCorners(
475-
@NonNull BitmapPool pool, @NonNull Bitmap inBitmap, int roundingRadius) {
476+
@NonNull BitmapPool pool, @NonNull Bitmap inBitmap, final int roundingRadius) {
476477
Preconditions.checkArgument(roundingRadius > 0, "roundingRadius must be greater than 0.");
477478

479+
return roundedCorners(
480+
pool,
481+
inBitmap,
482+
new DrawRoundedCornerFn() {
483+
@Override
484+
public void drawRoundedCorners(Canvas canvas, Paint paint, RectF rect) {
485+
canvas.drawRoundRect(rect, roundingRadius, roundingRadius, paint);
486+
}
487+
});
488+
}
489+
490+
/**
491+
* Creates a bitmap from a source bitmap and rounds the corners, applying a potentially different
492+
* [X, Y] radius to each corner.
493+
*
494+
* <p>This method does <em>NOT</em> resize the given {@link Bitmap}, it only rounds it's corners.
495+
* To both resize and round the corners of an image, consider {@link
496+
* com.bumptech.glide.request.RequestOptions#transform(Transformation[])} and/or {@link
497+
* com.bumptech.glide.load.MultiTransformation}.
498+
*
499+
* @param inBitmap the source bitmap to use as a basis for the created bitmap.
500+
* @param topLeft top-left radius
501+
* @param topRight top-right radius
502+
* @param bottomRight bottom-right radius
503+
* @param bottomLeft bottom-left radius
504+
* @return a {@link Bitmap} similar to inBitmap but with rounded corners.
505+
*/
506+
public static Bitmap roundedCorners(
507+
@NonNull BitmapPool pool,
508+
@NonNull Bitmap inBitmap,
509+
final float topLeft,
510+
final float topRight,
511+
final float bottomRight,
512+
final float bottomLeft) {
513+
return roundedCorners(
514+
pool,
515+
inBitmap,
516+
new DrawRoundedCornerFn() {
517+
@Override
518+
public void drawRoundedCorners(Canvas canvas, Paint paint, RectF rect) {
519+
Path path = new Path();
520+
path.addRoundRect(
521+
rect,
522+
new float[] {
523+
topLeft,
524+
topLeft,
525+
topRight,
526+
topRight,
527+
bottomRight,
528+
bottomRight,
529+
bottomLeft,
530+
bottomLeft
531+
},
532+
Path.Direction.CW);
533+
canvas.drawPath(path, paint);
534+
}
535+
});
536+
}
537+
538+
private static Bitmap roundedCorners(
539+
@NonNull BitmapPool pool, @NonNull Bitmap inBitmap, DrawRoundedCornerFn drawRoundedCornerFn) {
540+
478541
// Alpha is required for this transformation.
479542
Bitmap.Config safeConfig = getAlphaSafeConfig(inBitmap);
480543
Bitmap toTransform = getAlphaSafeBitmap(pool, inBitmap);
@@ -492,7 +555,7 @@ public static Bitmap roundedCorners(
492555
try {
493556
Canvas canvas = new Canvas(result);
494557
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
495-
canvas.drawRoundRect(rect, roundingRadius, roundingRadius, paint);
558+
drawRoundedCornerFn.drawRoundedCorners(canvas, paint, rect);
496559
clear(canvas);
497560
} finally {
498561
BITMAP_DRAWABLE_LOCK.unlock();
@@ -559,6 +622,12 @@ static void initializeMatrixForRotation(int exifOrientation, Matrix matrix) {
559622
}
560623
}
561624

625+
/** Convenience function for drawing a rounded bitmap. */
626+
private interface DrawRoundedCornerFn {
627+
628+
void drawRoundedCorners(Canvas canvas, Paint paint, RectF rect);
629+
}
630+
562631
private static final class NoLock implements Lock {
563632

564633
@Synthetic

0 commit comments

Comments
 (0)
Please sign in to comment.