I am trying to make a view that will have a background that is not only transparent, but will also have a blur effect. That way the views underneath appear to be out of focus. I want it to look like the screen does after holding down the power button. Any ideas?
3 Answers
Now that the window flag is deprecated, you've got to blur yourself. I answered this elsewhere but here is how you can blur a view:
You can now use ScriptIntrinsicBlur from the RenderScript library to blur quickly. Here is how to access the RenderScript API. The following is a class I made to blur Views and Bitmaps:
import android.support.v8.renderscript.*;
public class BlurBuilder {
private static final float BITMAP_SCALE = 0.4f;
private static final float BLUR_RADIUS = 7.5f;
public static Bitmap blur(View v) {
return blur(v.getContext(), getScreenshot(v));
}
public static Bitmap blur(Context ctx, 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(ctx);
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;
}
private static Bitmap getScreenshot(View v) {
Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.draw(c);
return b;
}
}
To apply this to a fragment, add the following to onCreateView
:
final Activity activity = getActivity();
final View content = activity.findViewById(android.R.id.content).getRootView();
if (content.getWidth() > 0) {
Bitmap image = BlurBuilder.blur(content);
window.setBackgroundDrawable(new BitmapDrawable(activity.getResources(), image));
} else {
content.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Bitmap image = BlurBuilder.blur(content);
window.setBackgroundDrawable(new BitmapDrawable(activity.getResources(), image));
}
});
}
NOTE: This solution requires min sdk to be API 17
EDIT: Renderscript is included into support v8 enabling this answer down to api 8. To enable it using gradle include these lines into your gradle file (from this answer) and use Renderscript from package android.support.v8.renderscript:
android {
...
defaultConfig {
...
renderscriptTargetApi *your target api*
renderscriptSupportModeEnabled true
}
...
}
-
3You rock. This seriously helped me out. One thing I added is the bitmap returned from the blur function is a TransitionDrawable. This helps to make the blur effect appear more smoothly. – Jessie A. Morris Sep 05 '14 at 19:34
-
1Care to comment how the BITMAP_SCALE and BLUR_RADIUS constants work? Thank you – Nick H Apr 28 '15 at 03:50
-
2This is not working for me. The background is not blurred at all – Teilmann Feb 04 '16 at 13:45
-
1To the ones who say it doesn't work... have you tried to use the view that you inflate in onCreateView() as target for the background instead of using the "window" ( activity.getWindow() ) ?? This worked for me – Noya May 26 '16 at 10:38
-
With a button click (with the code of your answer) I set the blur, but how can I delete it making click with another button? – Robert Jun 07 '16 at 23:16
-
1How to increase/decrease the blur effect? – Parag Kadam Dec 31 '16 at 10:39
-
Increase/decrease blur effect is achieved by modifying the values on `BITMAP_SCALE` and `BLUR_RADIUS` – Rui Cardoso Mar 16 '17 at 10:06
-
works. dont forget to remove the viewtreeobserver after or you'll leak. – j2emanue Jun 19 '17 at 03:51
-
3how to use it in activity ? – mrid Aug 22 '17 at 12:42
-
how to use it in AlerDialog or behind progressBar and progressDialog – MNFS Oct 27 '17 at 07:44
-
Thanks. I am looking for the same for alert dialog. @MNFS You can set this layout to root layout of dialog – Chirag Savsani Apr 19 '19 at 10:46
-
Is there any relation between BITMAP_SCALE or BLUR_RADIUS with the time it takes to blur the Bitmap? because I want to blur it as fast as it could. – Vijay Oct 02 '20 at 18:47
-
can anyone please tell me what is `window` in `window.setBackgroundDrawable(new BitmapDrawable(activity.getResources(), image));` – Simran Sharma Jun 02 '21 at 07:10
If all you want to do is blur the background of the window behind your activty then you can use this call from within your activity (or any class, like an AlertDialog that has a window)
getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Update: this flag was deprecated in Android 4.0 and no longer works.
-
-
3As others have said this is not longer supported, this post is 4 years old so follow B. Young's post instead. – Idistic May 01 '15 at 03:41
-
@Idistic, but if you follow B. Young's post you have to increase min SDK level to 17 :-/ – portfoliobuilder Aug 05 '15 at 23:19
Blurry is a nice option which will easily give you the effect you are looking for: https://github.com/wasabeef/Blurry
after adding it to project, wrap the view you want to blur in a FrameLayout, then use this line when you want to blur:
Blurry.with(this).radius(25).sampling(2).onto((ViewGroup) findViewById(R.id.viewToBlurWrapper));

- 85
- 7
-
1Unfortunatelly there is a problem with this lib https://github.com/wasabeef/Blurry/issues/50 Also in my app. – webrama.pl Oct 25 '18 at 11:29