Search code examples
androidandroid-activityandroid-blur-effectandroid-renderscript

How to make an activity background transparent and blur the background


When I launch my app, it launches an Activity which should have transparent header and whatever is the currently shown in background should be blurred.

enter image description here

I am able to get the transparency. But I am not able to figure out how to blur the background. For example if I launch app from home screen, then home screen should be visible but blurred out.

I have an idea to use Framebuffer to get current displayed data, but how to convert that to bitmap which I can use to draw an image without saving the image and directly using data.

I also know that We can take screenshot by pressing power and volume button. Does anyone has an idea where is the code in android to do that? My app will have system access.


Solution

  • how to blur the background?

    You can use RenderScript available in support library

    public class BlurBuilder {
        private static final float BITMAP_SCALE = 0.4f;
        private static final float BLUR_RADIUS = 7.5f;
    
        public static Bitmap blur(Context context, Bitmap image) {
            int width = Math.round(image.getWidth() * BITMAP_SCALE);
            int height = Math.round(image.getHeight() * BITMAP_SCALE);
    
            Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);
            Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);
    
            RenderScript rs = RenderScript.create(context);
            ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
            Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
            Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
            theIntrinsic.setRadius(BLUR_RADIUS);
            theIntrinsic.setInput(tmpIn);
            theIntrinsic.forEach(tmpOut);
            tmpOut.copyTo(outputBitmap);
    
            return outputBitmap;
        }
    }
    

    see this link for more details

    Or you can use Blurry

    Does anyone has an idea where is the code in android to do that?

    For taking screenshot of your app screen see this link