How to make an activity background transparent and blur the background

20k views Asked by At

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.

2

There are 2 answers

1
N J On BEST ANSWER

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

1
Junaid On

I used this to give a blur effect to my edit text background, you can modify it according to your preference, play around with opacity:

<?xml version="1.0" encoding="utf-8"?><!--  res/drawable/rounded_edittext.xml -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:centerColor="#33FFFFFF"
        android:endColor="#33FFFFFF"
        android:gradientRadius="270"
        android:startColor="#33FFFFFF"
        android:type="radial" />
    <corners
        android:bottomLeftRadius="25dp"
        android:bottomRightRadius="25dp"
        android:topLeftRadius="25dp"
        android:topRightRadius="25dp" />
</shape>

alternatively, you might be interested in this or this

As one more alternative you can use a small blur bitmap and repeat it:

    <xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    android:id="@+id/MainLayout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@drawable/backrepeat"
    >

Then prepare:

    <bitmap xmlns:android="http://schemas.android.com/apk/res/android"
        android:src="@drawable/back" 
        android:tileMode="repeat" />