创建模糊透明背景效果

74

我正在尝试创建一个视图,它不仅具有透明背景,还具有模糊效果。这样下面的视图看起来就会失去焦点。我希望它看起来像按下电源按钮后屏幕的样子。有什么想法吗?

3个回答

139

现在窗口标志已经被弃用,你需要模糊自己。我在其他地方回答过,但是以下是如何模糊视图的方法:

现在可以使用RenderScript库中的ScriptIntrinsicBlur来快速模糊。 这里是如何访问RenderScript API的方法。以下是我编写的一个用于模糊Views和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;
    }
}
在片段中应用此功能,请在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));
        }
    });
}

注意: 此解决方案需要最低的sdk版本为API 17

编辑: Renderscript已经包含在支持v8中,使得此答案适用于api 8。要在gradle中启用它,请将这些行包含到您的gradle文件中(来自于这个答案),并从android.support.v8.renderscript包中使用Renderscript:

android {
  ...
  defaultConfig {
    ...
    renderscriptTargetApi *your target api*
    renderscriptSupportModeEnabled true
  }
  ...
}

3
你太棒了。这对我非常有帮助。我增加的一件事是,模糊函数返回的位图是一个TransitionDrawable。这有助于使模糊效果看起来更加流畅。 - Jessie A. Morris
1
能否评论一下BITMAP_SCALE和BLUR_RADIUS常量是如何工作的?谢谢。 - Nick H
2
这对我不起作用。背景根本没有模糊。 - Teilmann
1
如何增加/减少模糊效果? - Parag Kadam
3
如何在活动中使用它? - mrid
显示剩余9条评论

16
如果你想要的只是模糊活动窗口后面的背景,那么你可以在你的活动中(或任何有窗口的类,如AlertDialog)使用以下调用:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);

更新:该标志在Android 4.0中已被弃用,不再起作用。


此功能已不再受支持。 - Kaustubh
3
正如其他人所说,这已不再受支持。此帖子已有4年之久,因此请遵循B. Young的帖子。 - Idistic
@Idistic,但是如果你按照B.Young的帖子操作,你需要将最小SDK级别提高到17 :-/ - portfoliobuilder

0

Blurry是一个不错的选择,可以轻松地给你想要的效果: https://github.com/wasabeef/Blurry

将它添加到项目中后,将想要模糊的视图包装在FrameLayout中,然后当您想要进行模糊处理时使用以下代码:

Blurry.with(this).radius(25).sampling(2).onto((ViewGroup) findViewById(R.id.viewToBlurWrapper));

1
很遗憾,这个库 https://github.com/wasabeef/Blurry/issues/50 存在问题。我的应用程序也有同样的问题。 - webrama.pl

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接