Android 模糊警报对话框后面的背景

Android 模糊警报对话框后面的背景,android,android-alertdialog,blur,Android,Android Alertdialog,Blur,正如我们从API 14知道的,下面的模糊已经被弃用 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND); 有没有其他方法可以使对话框后面的屏幕模糊 我已经试过了试一下这个方法会对你有帮助我正在使用这个 创建styles.xml文件 <style name="Theme.D1NoTitleDim" parent="android:style/Theme.Translucent">

正如我们从API 14知道的,下面的模糊已经被弃用

dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND); 
有没有其他方法可以使对话框后面的屏幕模糊
我已经试过了

试一下这个方法会对你有帮助我正在使用这个

创建styles.xml文件

<style name="Theme.D1NoTitleDim" parent="android:style/Theme.Translucent">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:backgroundDimEnabled">true</item>
    <item name="android:background">@android:color/transparent</item>        
</style>
这对我来说很好


有关更多信息,我建议您查看此更新:

我们还可以使用
renderscript
api对api级别>=17的背景进行模糊,并对api级别<17的背景进行快速模糊。请查找下面的Github项目

使用快速模糊的步骤

1) 使用下面的代码拍摄您的背景快照

private static Bitmap takeScreenShot(Activity activity)
    {
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap b1 = view.getDrawingCache();
        Rect frame = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        int statusBarHeight = frame.top;
        int width = activity.getWindowManager().getDefaultDisplay().getWidth();
        int height = activity.getWindowManager().getDefaultDisplay().getHeight();

        Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height  - statusBarHeight);
        view.destroyDrawingCache();
        return b;
    }
像这样使用它
Bitmap=takeScreenShot(BlurImageView.This)//您的活动名称

2) 调用方法
Bitmap fast=fastblur(map,10)

就这样,现在你可以看到你活动背后的模糊图像

希望这对任何人都有帮助。

此解决方案的工作原理如下所示

但是,如果您不能向中心发出警报(像我一样),请检查我的答案, 那是

我所做的是

使用了两个
alertDialogs
一个用于模糊效果(用于
getWindow()
),现在无法显示(我删除了它的
setContentView
/可以使用透明背景)

另一个是真实的
警报对话框
在模糊效果后显示

输出


为了快速模糊并解决屏幕顶部出现对话框的问题,我认为有一个更简单的解决方案:

// Custom Dialog to be called in the Activity choosed.
public class CustomDialog {
    private Dialog dialog;

    public CustomDialog(AppCompatActivity activity) {
        dialog = new Dialog(activity);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        View viewCustomDialog = LayoutInflater.from(activity).inflate(R.layout.custom_dialog, null);
        dialog.setContentView(viewCustomDialog);

        RelativeLayout rootView = viewCustomDialog.findViewById(R.id.rootView);

        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        layoutParams.gravity = Gravity.CENTER;
        dialog.getWindow().setAttributes(layoutParams);

        // First get bitmap with blur filter applied, using the function blur presented here,  
        // or another function.
        // Activity parameter is the Activity for which you call dialog.show();
        Bitmap bitmap = Utils.blur(activity);

        // Get bitmap height.
        int bitmapHeight = bitmap.getHeight();

        dialog.setOnShowListener(dialogInterface -> {
            // When dialog is shown, before set new blurred image for background drawable, 
            // the root view height and dialog margin are saved.
            int rootViewHeight = rootView.getHeight();
            int marginLeftAndRight = dialog.getWindow().getDecorView().getPaddingLeft();

            // Set new blurred image for background drawable.
            dialog.getWindow().setBackgroundDrawable(new BitmapDrawable(builder.context.getResources(), bitmap));

            // After get positions and heights, recover and rebuild original marginTop position, 
            // that is (bitmapHeight - rootViewHeight) / 2.
            // Also recover and rebuild Dialog original left and right margin.
            FrameLayout.LayoutParams rootViewLayoutParams = (FrameLayout.LayoutParams)rootView.getLayoutParams();
            rootViewLayoutParams.setMargins(marginLeftAndRight, (bitmapHeight - rootViewHeight) / 2, marginLeftRight, 0);
        });

        dialog.show();
    }
}
模糊方法:

public static Bitmap blur(AppCompatActivity activity) {
   Bitmap bitmap = Utils.takeScreenShot(activity);

   RenderScript renderScript = RenderScript.create(activity);

   // This will blur the bitmapOriginal with a radius of 16 and save it in bitmapOriginal
   final Allocation input = Allocation.createFromBitmap(renderScript, bitmap); // Use this constructor for best performance, because it uses USAGE_SHARED mode which reuses memory
   final Allocation output = Allocation.createTyped(renderScript, input.getType());
   final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript));
   script.setRadius(16f);
   script.setInput(input);
   script.forEach(output);
   output.copyTo(bitmap);

   return bitmap;
}
截图方法:

public static Bitmap takeScreenShot(AppCompatActivity activity) {
    View view = activity.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    Bitmap bitmap = view.getDrawingCache();
    Rect frame = new Rect();
    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
    int statusBarHeight = frame.top;
    int[] widthHeight = getScreenSize(activity);
    Bitmap bitmapResult = Bitmap.createBitmap(bitmap, 0, statusBarHeight, widthHeight[0], widthHeight[1]  - statusBarHeight);
    view.destroyDrawingCache();
    return bitmapResult;
}   
GetScreenSize方法:

public static int[] getScreenSize(Context context) {
    int[] widthHeight = new int[2];
    widthHeight[WIDTH_INDEX] = 0;
    widthHeight[HEIGHT_INDEX] = 0;

    WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = windowManager.getDefaultDisplay();

    Point size = new Point();
    display.getSize(size);
    widthHeight[WIDTH_INDEX] = size.x;
    widthHeight[HEIGHT_INDEX] = size.y;

    if (!isScreenSizeRetrieved(widthHeight)) {
        DisplayMetrics metrics = new DisplayMetrics();
        display.getMetrics(metrics);
        widthHeight[0] = metrics.widthPixels;
        widthHeight[1] = metrics.heightPixels;
    }

    if (!isScreenSizeRetrieved(widthHeight)) {
        widthHeight[0] = display.getWidth(); // deprecated
        widthHeight[1] = display.getHeight(); // deprecated
    }

    return widthHeight;
}   

private static boolean isScreenSizeRetrieved(int[] widthHeight) {
    return widthHeight[WIDTH_INDEX] != 0 && widthHeight[HEIGHT_INDEX] != 0;
}
要使用RenderScript(模糊),必须在应用程序的build.gradle中添加:

defaultConfig {
    ...     
    renderscriptTargetApi 19
    renderscriptSupportModeEnabled true
}
试试这个

    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(view);
    dialog.setCancelable(true);
    Window window = dialog.getWindow();
    WindowManager.LayoutParams wlp = window.getAttributes();

    wlp.gravity = Gravity.CENTER;
    wlp.flags &= WindowManager.LayoutParams.FLAG_DIM_BEHIND;
    window.setAttributes(wlp);
    dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    dialog.show();
}

@嗨,Gaurav Thks,但它使背景变暗而不模糊…任何其他建议请提供您想要的任何图像,请张贴that@nit这不再意味着使dim成为唯一可能的解决方案,您可以在xml文件中创建自己的对话框。。使用模糊的PNG使其产生这种效果。。布局之上的布局。此实现是否会导致应用程序出现某些滞后??当我尝试实现此功能时,应用程序有点慢。@user2247689这是因为fastBlur方法需要一点时间才能实现。你找到了解决此问题的方法吗??顺便说一句
back\u dim\u layout.setVisibility(View.GONE)
here?@user2247689您可以尝试此教程作为参考。它实际上存储图像以使其模糊。
getDefaultDisplay().getWidth()
getDefaultDisplay().getHeight()
不推荐使用。使用
getRealMetrics()
方法:尝试更详细地解释您的答案,而不仅仅是提供代码。你的代码为什么工作?为什么这是首选的解决方案?
public static int[] getScreenSize(Context context) {
    int[] widthHeight = new int[2];
    widthHeight[WIDTH_INDEX] = 0;
    widthHeight[HEIGHT_INDEX] = 0;

    WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = windowManager.getDefaultDisplay();

    Point size = new Point();
    display.getSize(size);
    widthHeight[WIDTH_INDEX] = size.x;
    widthHeight[HEIGHT_INDEX] = size.y;

    if (!isScreenSizeRetrieved(widthHeight)) {
        DisplayMetrics metrics = new DisplayMetrics();
        display.getMetrics(metrics);
        widthHeight[0] = metrics.widthPixels;
        widthHeight[1] = metrics.heightPixels;
    }

    if (!isScreenSizeRetrieved(widthHeight)) {
        widthHeight[0] = display.getWidth(); // deprecated
        widthHeight[1] = display.getHeight(); // deprecated
    }

    return widthHeight;
}   

private static boolean isScreenSizeRetrieved(int[] widthHeight) {
    return widthHeight[WIDTH_INDEX] != 0 && widthHeight[HEIGHT_INDEX] != 0;
}
defaultConfig {
    ...     
    renderscriptTargetApi 19
    renderscriptSupportModeEnabled true
}
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(view);
    dialog.setCancelable(true);
    Window window = dialog.getWindow();
    WindowManager.LayoutParams wlp = window.getAttributes();

    wlp.gravity = Gravity.CENTER;
    wlp.flags &= WindowManager.LayoutParams.FLAG_DIM_BEHIND;
    window.setAttributes(wlp);
    dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    dialog.show();
}