美文网首页
Glide设置圆角图片后设置ImageVIew的scanType

Glide设置圆角图片后设置ImageVIew的scanType

作者: Solang | 来源:发表于2018-07-19 16:17 被阅读498次

    Glide3.0 :
    出现原因:看centerCrop()方法的源码可知,也是需要调用transform()方法的.所以前后共用CenterCrop会覆盖掉GlideRoundImage的效果

    public BitmapRequestBuilder<ModelType, TranscodeType> centerCrop() {
            return transform(glide.getBitmapCenterCrop());
        }
    

    解决办法:2个效果同时放在transform里面就可以解决问题了。

    .transform(new CenterCrop(Context context),new GlideRoundTransform(Context context,5))
    

    Glide4.0
    使用了glide4.0.0 +后,.transform方法参数改变,不能添加new CenterCrop(Context context)方法,修改GlideRoundTransform 继承CenterCrop 不在继承BitmapTransformation 如下:

    public class GlideRoundTransform extends CenterCrop {
    
        private static float radius = 10f;
        public GlideRoundTransform(Context context) {
            this(context,10);
        }
        public GlideRoundTransform(Context context, int dp) {
            super(context);
            this.radius = DensityUtils.dp2px(context,dp);
        }
    
        @Override
        protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
            //glide4.0+
            Bitmap transform = super.transform(pool, toTransform, outWidth, outHeight);
            return roundCrop(pool, transform);
            //glide3.0
            //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;
        }
    
        public String getId() {
            return getClass().getName() + Math.round(radius);
        }
    
        @Override
        public void updateDiskCacheKey(MessageDigest messageDigest) {
    
        }
    

    然后引用:

    .transform(new GlideRoundTransform(mContext,5))
    

    就可以正常显示了

    相关文章

      网友评论

          本文标题:Glide设置圆角图片后设置ImageVIew的scanType

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