9

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.

N J
  • 27,217
  • 13
  • 76
  • 96
VinayChoudhary99
  • 846
  • 3
  • 13
  • 26

2 Answers2

6

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

Community
  • 1
  • 1
N J
  • 27,217
  • 13
  • 76
  • 96
  • 6
    It takes bitmap as an input. My problem is that whatever is behind my activity, e.g, home screen, should get blurred, I tried getting bitmap from my activity root view but that simply return me my view's background (which i set to a transparent with an image) and not home screen. I want something like WindowManager.LayoutParams.FLAG_BLUR_BEHIND which would have blurred anything in background. How can i do this? – VinayChoudhary99 Aug 19 '15 at 08:15
4

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" />
Skynet
  • 7,820
  • 5
  • 44
  • 80