Create a blurred background effect

综合技术 2017-11-15

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?

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:


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);

        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);
        return b;

To apply this to a fragment, add the following to onCreateView

final Activity activity = getActivity();
final View content = activity.findViewById(;
if (content.getWidth() > 0) {
    Bitmap image = BlurBuilder.blur(content);
    window.setBackgroundDrawable(new BitmapDrawable(activity.getResources(), image));
} else {
    content.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        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 {
  defaultConfig {
    renderscriptTargetApi *your target api*
    renderscriptSupportModeEnabled true


Android内存优化之图片优化 一般来说,图片是APP占用内存高的主要原因,所以优化图片的内存占用是避免OOM的根本手段。对于图片占用的内存,我们可能总有这样的误区: 图片本身所占的存储空间越小,占用的内存越小。所以认为只要将图片进行压缩,就相当于减小了内存占用 。其实这是不对的,图片占用的存储空间的大小与所占的内存...
Android 资源别名(alias resources)使用小记 第一次写文章,主要是分享一下,在网上搜索了很久发现没有相关文章,有不好的地方请大家指出。 从Android 开发者官网 得知到资源别名的使用。如果将此文件另存为 icon.xml(例如,在备用资源目录中,另存为 res/drawable-en-rCA...
drawable,bitmap,base64,file之间的转换 作者:某人_Valar 如需转载请保留原文链接; 不断整理中,大家有好的方法也可以在评论中写出。 drawable与bitmap之间转换 /** * drawable转为bitmap *///drawable文件夹下的文件res直接转...
Draw the spiral of Theodorus in C# The spiral of Theodorus (which is also called the square root spiral, Einstein spiral, and Pythagorean spiral) was first devised by the Greek ...
长文【Android模糊图片处理】 现在许多app中都应用到了对图片的模糊处理,图片高斯模糊备受设计师的青睐,在各大知名APP中,如微信、手机QQ、网易云音乐等等都有对背景高斯图模糊的设计,还有今日头条、内涵段子等。 在Android 中,现在常用的图片高斯模糊技术的根本有两种:RenderScri...