Glide CenterCrop与Transformer的共存问题

转载自:https://www.jianshu.com/p/ef0ed5b53eb1

1.CenterCrop与Transformer的共存问题

因为此bug无法在使用GridLayoutManager和StaggeredGridLayoutManager等其他情况中使用.centerCrop选项,所以要实现此功能需要在ImageView中去设置scaleType为centerCrop

But,如果你想同时让图片有圆角之类的Transformer,比如在Glide中.transform()配置了一个圆角矩形,那如果同时ImageView的scaleType设置了centerCrop,那圆角就没有了

要解决此问题需要设置两个Transformer,在我的项目中像这样:

.transform(new CenterCrop(getContext())
          ,new GlideRoundTransform(getContext(), 20))

这样完美解决问题。
附上圆角的代码:
GlideRoundTransform 代码

https://github.com/wasabeef/glide-transformations/issues/16
https://github.com/bumptech/glide/issues/613


 * Created by rainfool on 16/8/4.
 */
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);
    }
}

小礼物走一走,来简书关注我



作者:RainFool
链接:https://www.jianshu.com/p/0e79080c8116
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值