Glide used to change the picture fillet

1. The following is not what you want to look at the effect (the four corners are rounded, as there are upper left, upper right corner of the fillet), while also comes pre-loaded and the effect of failure to load the picture

2. Before using Glide introduced us to rely

implementation 'com.github.bumptech.glide:glide:4.9.0'

3. Then create a class change rounded GildeRoundTransform

 1 public class GlideRoundTransform extends BitmapTransformation {
 2     private static float radius = 0f;
 3 
 4     public GlideRoundTransform() {
 5         this(4);
 6     }
 7 
 8     public GlideRoundTransform(int dp) {
 9         super();
10         this.radius = Resources.getSystem().getDisplayMetrics().density * dp;
11     }
12 
13 
14     @Override
15     protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
16         //变换的时候裁切
17         Bitmap bitmap = TransformationUtils.centerCrop(pool, toTransform, outWidth, outHeight);
18         return roundCrop(pool, bitmap);
19     }
20 
21     @Override
22     public void updateDiskCacheKey(MessageDigest messageDigest) {
23 
24     }
25 
26 
27     private static Bitmap roundCrop(BitmapPool pool, Bitmap source) {
28         if (source == null) {
29             return null;
30         }
31         Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
32         if (result == null) {
33             result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
34         }
35         Canvas canvas = new Canvas(result);
36         Paint paint = new Paint();
37         paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
38         paint.setAntiAlias(true);
39         RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
40         canvas.drawRoundRect(rectF, radius, radius, paint);
41         //左上角、右上角圆角
42 //        RectF rectRound = new RectF(0f, 100f, source.getWidth(), source.getHeight());
43 //        canvas.drawRect(rectRound, paint);
44         return result;
45     }
46 }

4. Then where needed references

. 1 RequestOptions Options = new new RequestOptions ()
 2                  .centerCrop ()
 . 3                  .placeholder (R.mipmap.ic_launcher_round) // preloaded image 
. 4                  .error (R.drawable.ic_launcher_foreground) // load failure image 
. 5                  .priority (Priority.HIGH ) // priority 
. 6                  .diskCacheStrategy (DiskCacheStrategy.NONE) // buffer 
. 7                  .transform ( new new GlideRoundTransform (. 5)); // fillet 
8 Glide.with(context).load(list.get(position).getImage()).apply(options).into(holder.imageView);

5. GlideRoundTransform class rows 42 and 43, to change the lower-left corner, lower right corner of the right angle effect, attached renderings

 

Guess you like

Origin www.cnblogs.com/Mr-Deng/p/11532586.html