Android 创建模糊透明的背景效果

Android 创建模糊透明的背景效果,android,android-layout,Android,Android Layout,我试图制作一个背景不仅透明,而且具有模糊效果的视图。这样,下面的视图看起来就失去了焦点。我想让它看起来像按下电源按钮后的屏幕。有什么想法吗?如果您只想模糊活动背后窗口的背景,那么您可以在活动(或任何类,如有窗口的AlertDialog)中使用此调用 更新:该标志在Android 4.0中已被弃用,不再起作用既然窗口标志已被弃用,您必须模糊自己。我在其他地方回答了这个问题,但以下是模糊视图的方法: 现在可以从RenderScript库使用快速模糊。是如何访问RenderScript API的。以下

我试图制作一个背景不仅透明,而且具有模糊效果的视图。这样,下面的视图看起来就失去了焦点。我想让它看起来像按下电源按钮后的屏幕。有什么想法吗?

如果您只想模糊活动背后窗口的背景,那么您可以在活动(或任何类,如有窗口的AlertDialog)中使用此调用


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

既然窗口标志已被弃用,您必须模糊自己。我在其他地方回答了这个问题,但以下是模糊视图的方法:

现在可以从RenderScript库使用快速模糊。是如何访问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;
    }
}
要将此应用于片段,请将以下内容添加到
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));
        }
    });
}
注意:此解决方案要求min sdk为API 17

EDIT:Renderscript包含在支持v8中,支持api 8的这一答案。要使用gradle启用它,请将这些行包括到您的gradle文件中(从此处),并使用程序包android.support.v8.Renderscript中的Renderscript:


模糊是一个很好的选择,它可以很容易地为您提供您想要的效果:

将其添加到project后,将要模糊的视图包装到FrameLayout中,然后在要模糊时使用此行:

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

正是我想要的。我很感激!这不再受支持。正如其他人所说这不再受支持,这篇文章已经有4年历史了,所以请改为跟随B.Young的文章。@Idistic,但如果你跟随B.Young的文章,你必须将min SDK级别提高到17:-/你太棒了。这帮了我很大的忙。我添加的一点是,从blur函数返回的位图是TransitionDrawable。这有助于使模糊效果看起来更平滑。是否要评论位图缩放和模糊半径常量的工作原理?谢谢这对我不起作用。对于那些说它不起作用的人来说,背景一点也不模糊。。。您是否尝试使用在onCreateView()中膨胀的视图作为背景的目标,而不是使用“窗口”(activity.getWindow())??这对我来说很有效。如何在活动中使用它?不幸的是,我的应用程序中也存在这个库的问题。
android {
  ...
  defaultConfig {
    ...
    renderscriptTargetApi *your target api*
    renderscriptSupportModeEnabled true
  }
  ...
}
Blurry.with(this).radius(25).sampling(2).onto((ViewGroup) findViewById(R.id.viewToBlurWrapper));