7
7
import android .graphics .Color ;
8
8
import android .graphics .Matrix ;
9
9
import android .graphics .Paint ;
10
+ import android .graphics .Path ;
10
11
import android .graphics .PorterDuff ;
11
12
import android .graphics .PorterDuffXfermode ;
12
13
import android .graphics .RectF ;
@@ -472,9 +473,71 @@ public static Bitmap roundedCorners(
472
473
* @throws IllegalArgumentException if roundingRadius, width or height is 0 or less.
473
474
*/
474
475
public static Bitmap roundedCorners (
475
- @ NonNull BitmapPool pool , @ NonNull Bitmap inBitmap , int roundingRadius ) {
476
+ @ NonNull BitmapPool pool , @ NonNull Bitmap inBitmap , final int roundingRadius ) {
476
477
Preconditions .checkArgument (roundingRadius > 0 , "roundingRadius must be greater than 0." );
477
478
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
+
478
541
// Alpha is required for this transformation.
479
542
Bitmap .Config safeConfig = getAlphaSafeConfig (inBitmap );
480
543
Bitmap toTransform = getAlphaSafeBitmap (pool , inBitmap );
@@ -492,7 +555,7 @@ public static Bitmap roundedCorners(
492
555
try {
493
556
Canvas canvas = new Canvas (result );
494
557
canvas .drawColor (Color .TRANSPARENT , PorterDuff .Mode .CLEAR );
495
- canvas . drawRoundRect ( rect , roundingRadius , roundingRadius , paint );
558
+ drawRoundedCornerFn . drawRoundedCorners ( canvas , paint , rect );
496
559
clear (canvas );
497
560
} finally {
498
561
BITMAP_DRAWABLE_LOCK .unlock ();
@@ -559,6 +622,12 @@ static void initializeMatrixForRotation(int exifOrientation, Matrix matrix) {
559
622
}
560
623
}
561
624
625
+ /** Convenience function for drawing a rounded bitmap. */
626
+ private interface DrawRoundedCornerFn {
627
+
628
+ void drawRoundedCorners (Canvas canvas , Paint paint , RectF rect );
629
+ }
630
+
562
631
private static final class NoLock implements Lock {
563
632
564
633
@ Synthetic
0 commit comments