Glide4.7.1 圆角与centerCrop冲突问题

问题:在glide配置中使用options.transforms(new GlideRoundedCornersTransform(context,5));来实现圆角以及centerCrop效果时,刷新界面会出现闪烁
解决方法:由于glide默认采用了centerCrop方式显示图片,所以基本不需要再次设置,而圆角采用自定义控件的方式就可以实现其效果并不会出现闪烁问题

当然如果不介意可以使用下面方法:

public class GlideRoundTransform extends CenterCrop {

    private static float radius = 10f;

    public GlideRoundTransform(Context context) {
        this(context, 10);
    }

    public GlideRoundTransform(Context context, int dp) {
        super();
        this.radius = UnitConversionUtil.dip2px(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) {

    }
}

猜你喜欢

转载自blog.csdn.net/baidu_21345205/article/details/84562804
今日推荐