Android Glide高斯模糊加载图片

在这里插入图片描述

      Glide.with(this)
                .load(R.mipmap.bg_default_cover)
                .crossFade(1000)
                .bitmapTransform(new BlurTransformation(EditVideoStoryActivity.this)) 
                .into(ivBg);
 public class BlurTransformation extends BitmapTransformation {
        private RenderScript rs;

        public BlurTransformation(Context context) {
            super( context );
            rs = RenderScript.create( context );
        }

        @Override
        protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
            Bitmap blurredBitmap = toTransform.copy( Bitmap.Config.ARGB_8888, true );

            // Allocate memory for Renderscript to work with
            Allocation input = Allocation.createFromBitmap(
                    rs,
                    blurredBitmap,
                    Allocation.MipmapControl.MIPMAP_FULL,
                    Allocation.USAGE_SHARED
            );
            Allocation output = Allocation.createTyped(rs, input.getType());

            // Load up an instance of the specific script that we want to use.
            ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
            script.setInput(input);

            // Set the blur radius
            script.setRadius(10);

            // Start the ScriptIntrinisicBlur
            script.forEach(output);

            // Copy the output to the blurred bitmap
            output.copyTo(blurredBitmap);

            toTransform.recycle();

            return blurredBitmap;
        }

        @Override
        public String getId() {
            return "blur";
        }
    }

猜你喜欢

转载自blog.csdn.net/yu540135101/article/details/84028613