android Glide4.0 设置centercrop 与高斯模糊效果 冲突的问题

原因:Glide 4.0 的centerCrop 方法 内部也是执行的transform方法,所以在执行了centerCrop后再调用transform方法,会被覆盖

解决方法:重写BlurTransformer 类

public class GlideBlurTransformer extends BitmapTransformation {
    private int radius;
    private int sampling;
    private Context context;

    public GlideBlurTransformer(Context context,int radius, int sampling) {
        this.context = context;
        this.radius = radius;
        this.sampling = sampling;
    }

    @Override
    protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
        Bitmap bitmap = TransformationUtils.centerCrop(pool, toTransform, outWidth, outHeight);
        return blurCrop(pool,bitmap);
    }

    private Bitmap blurCrop(BitmapPool pool, Bitmap source) {
        if (source == null) return null;
        int width = source.getWidth();
        int height = source.getHeight();
        int scaledWidth = width / sampling;
        int scaledHeight = height / sampling;

        Bitmap bitmap = pool.get(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(bitmap);
        canvas.scale(1 / (float) sampling, 1 / (float) sampling);
        Paint paint = new Paint();
        paint.setFlags(Paint.FILTER_BITMAP_FLAG);
        canvas.drawBitmap(source, 0, 0, paint);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            try {
                bitmap = RSBlur.blur(context, bitmap, radius);
            } catch (RSRuntimeException e) {
                bitmap = FastBlur.blur(bitmap, radius, true);
            }
        } else {
            bitmap = FastBlur.blur(bitmap, radius, true);
        }

        return bitmap;
    }

    @Override
    public void updateDiskCacheKey(MessageDigest messageDigest) {

    }
}

首先获取到裁剪的图片,在进行高斯模糊

 

 高斯模糊效果android Glide4.0 设置centercrop 与高斯模糊效果 冲突的问题_第1张图片

你可能感兴趣的:(android Glide4.0 设置centercrop 与高斯模糊效果 冲突的问题)