美文网首页
解决Glide4.0圆角和CenterCrop不能共存的问题

解决Glide4.0圆角和CenterCrop不能共存的问题

作者: 43d60efa37c7 | 来源:发表于2017-08-09 10:52 被阅读1562次
    public class CenterCropRoundCornerTransform extends CenterCrop {
    
        private int radius = 0;
    
        public CenterCropRoundCornerTransform(int radius){
            this.radius = radius;
        }
    
        @Override
        protected Bitmap transform(BitmapPool pool, Bitmap toTransform,
                                   int outWidth, int outHeight) {
            Bitmap transform = super.transform(pool, toTransform, outWidth, outHeight);
            return roundCrop(pool, transform);
        }
    
        private 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;
        }
    
    }
    

    使用(Kotlin代码)

    var options = RequestOptions()
    options.transform(CenterCropRoundCornerTransform(Utils.dp2px(activity,5)))
    Glide.with(this)
                    .load("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1502254525934&di=bc87af364c11e1e23183246c0a0d27da&imgtype=0&src=http%3A%2F%2Fimg5q.duitang.com%2Fuploads%2Fitem%2F201406%2F10%2F20140610013326_4zPtP.jpeg")
                    .apply(options)
                    .into(iv_user_icon)
    

    相关文章

      网友评论

          本文标题:解决Glide4.0圆角和CenterCrop不能共存的问题

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