创建模糊的透明背景效果

b_y*_*yng 69 android android-layout

我正在尝试制作一个视图,其背景不仅透明,而且还具有模糊效果.这样,下面的视图似乎没有焦点.按住电源按钮,我希望它看起来像屏幕一样.有任何想法吗?

b_y*_*yng 132

现在窗口标志已被弃用,你必须自己模糊.我在其他地方回答了这个问题,但这里是你如何模糊视图:

您现在可以使用RenderScript库中的ScriptIntrinsicBlur快速模糊.以下是访问RenderScript API的方法.以下是我用来模糊视图和位图的类:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

要将其应用于片段,请将以下内容添加到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));
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

注意:此解决方案要求min sdk为API 17

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

android {
  ...
  defaultConfig {
    ...
    renderscriptTargetApi *your target api*
    renderscriptSupportModeEnabled true
  }
  ...
}
Run Code Online (Sandbox Code Playgroud)

  • 你摇滚。这确实帮助了我。我添加的一件事是从模糊函数返回的位图是一个 TransitionDrawable。这有助于使模糊效果显得更加平滑。 (3认同)
  • 什么是"窗口"? (3认同)
  • 如何在活动中使用它? (3认同)
  • 这不适合我.背景并不模糊 (2认同)

Idi*_*tic 16

如果你想做的只是模糊你的活动后面窗口的背景,那么你可以在你的活动中使用这个调用(或者任何类,比如有一个窗口的AlertDialog)

getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Run Code Online (Sandbox Code Playgroud)

更新:此标志在Android 4.0中已弃用,不再有效.

  • 这已被弃用,不再适用于Android L. (4认同)
  • 正如其他人所说的那样,这个帖子已经有4年了,所以请关注B. Young的帖子. (3认同)