How to blur background images in Android

前端 未结 10 2120
借酒劲吻你
借酒劲吻你 2020-11-30 22:12

What is the best way to blur background images like the image below? I saw some code and libraries but their are a couple of years old or like BlurBehind library, but it doe

10条回答
  •  一生所求
    2020-11-30 22:56

    The simplest way to achieve this is given below,

    I)

    Glide.with(context.getApplicationContext())
                            .load(Your Path)   
                            .override(15, 15) // (change according to your wish)
                            .error(R.drawable.placeholder)
                            .into(image.score);
    

    else you can follow the code below..

    II)

    1.Create a class.(Code is given below)

    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";
        }
    }
    

    2.Set image to ImageView using Glide.

    eg:

    Glide.with(this)
         .load(expertViewDetailsModel.expert.image)
         .asBitmap()
         .transform(new BlurTransformation(this))
         .into(ivBackground);
    

提交回复
热议问题