美文网首页
glide实现圆角展示

glide实现圆角展示

作者: 我是你森哥哥 | 来源:发表于2017-06-09 15:01 被阅读0次

    在项目中加上这个类

    public class GlideRoundTransform extends BitmapTransformation {
        private static float radius = 0f;
    
        public GlideRoundTransform(Context context) {
            this(context, 4);
        }
    
        public GlideRoundTransform(Context context, int dp) {
            super(context);
            this.radius = Resources.getSystem().getDisplayMetrics().density * dp;
        }
    
        @Override protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
            return roundCrop(pool, toTransform);
        }
    
        private static Bitmap roundCrop(BitmapPool pool, Bitmap source) {
            if (source == null) return null;
    
            Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
            if (result == null) {
                result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
            }
    
            Canvas canvas = new Canvas(result);
            Paint paint = new Paint();
            paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
            paint.setAntiAlias(true);
            RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
            canvas.drawRoundRect(rectF, radius, radius, paint);
            return result;
        }
    
        @Override public String getId() {
            return getClass().getName() + Math.round(radius);
        }
    }
    
    
    

    使用方法

     Glide.with(context)
                    .load(imageUrl)
                    .transform(new CenterCrop(context), new GlideRoundTransform(context,i))
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .crossFade()
                    .into(view);
    

    相关文章

      网友评论

          本文标题:glide实现圆角展示

          本文链接:https://www.haomeiwen.com/subject/nicfqxtx.html