• 使用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行,改变左下角,右下角为直角的效果,附上效果图

  • 相关阅读:
    Android weight属性详解
    设计模式(一)__单例设计模式
    Java中线程的生命周期
    抽象类和接口
    SQL sever 怎样将DBF文件导入到数据库
    JS去除字符串中空格,及常用正则表达式
    Oracle 11g问题1:关于error:ORA12541: TNS: 没有监听器
    access、excel取随机n条记录
    tsql字符串操作
    测试SQL Server执行时间和CPU时间
  • 原文地址:https://www.cnblogs.com/Mr-Deng/p/11532586.html
Copyright © 2020-2023  润新知