zoukankan      html  css  js  c++  java
  • 使用Glide改变图片的圆角

    1.先看以下是不是您想要的效果(四个角都是圆角,以下还有左上,右上角的圆角),同时还附带预加载以及加载失败图片的效果

    2.使用Glide之前我们先引入依赖

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

    3.然后创建一个改变圆角的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.然后在需要的地方引用

    1 RequestOptions options = new RequestOptions()
    2                 .centerCrop()
    3                 .placeholder(R.mipmap.ic_launcher_round) //预加载图片
    4                 .error(R.drawable.ic_launcher_foreground) //加载失败图片
    5                 .priority(Priority.HIGH) //优先级
    6                 .diskCacheStrategy(DiskCacheStrategy.NONE) //缓存
    7                 .transform(new GlideRoundTransform(5)); //圆角
    8 Glide.with(context).load(list.get(position).getImage()).apply(options).into(holder.imageView);

    5.在GlideRoundTransform类中42、43行,改变左下角,右下角为直角的效果,附上效果图

  • 相关阅读:
    网上零售是国内品牌开拓海外市场的最佳途径
    一个都不能少,海外B2C实战攻略全解
    我想和你一起浅浅淡淡的生活
    中国十大电子商务网站排名
    外贸B2C必读:外贸B2C入行指南(一)
    外贸B2C必读:外贸B2C入行指南(二)
    09年美国最热门的100个B2C网站,他们是怎么成功的?
    几个jsp模块
    【Quartz】【程序目录结构】/DetectNonWorkingDay/src/main/java/com/apple/sqm/dnwd/detect/delta/Detect.java
    Servlet 工作原理解析
  • 原文地址:https://www.cnblogs.com/Mr-Deng/p/11532586.html
Copyright © 2011-2022 走看看