android - 对话框背后的模糊背景

标签 android android-alertdialog android-dialogfragment

我想要对话框下面有模糊的屏幕,所以我拍摄了 Activity 的“屏幕截图”,对其进行模糊处理并将其设置为对话框窗口的背景,如 BitmapDrawable。奇怪的是,对话框不再以屏幕为中心,即使调用了 setCanceledOnTouchOutside(true),触摸外部对话框也不会关闭它。

问题是:为什么这不起作用?分别如何创建背景模糊的对话框?

public class BlurDialog extends DialogFragment {

public BlurDialog() {
}

public static BlurDialog newInstance() {
    return new BlurDialog();
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final AlertDialog alertDialog = new AlertDialog.Builder(getActivity())
            .setTitle("Title")
            .setMessage("Message")
            .setPositiveButton("OK", null)
            .setNegativeButton("Cancel", null)
            .create();
    alertDialog.setCanceledOnTouchOutside(true);


    View view = getActivity().getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    Bitmap b1 = view.getDrawingCache();

    Rect frame = new Rect();
    getActivity().getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
    int statusBarHeight = frame.top;
    final int width = getActivity().getWindow().getDecorView().getWidth();
    final int height = getActivity().getWindow().getDecorView().getHeight();

    Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height-statusBarHeight);

    //define this only once if blurring multiple times
    RenderScript rs = RenderScript.create(getActivity());

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

    alertDialog.getWindow().setBackgroundDrawable(new BitmapDrawable(getResources(), b));


    return alertDialog;
}
}

Screenshot

最佳答案

这篇文章很旧,但供您引用:

要创建背景模糊的对话框,你可以使用这个库:

https://github.com/tvbarthel/BlurDialogFragment

您可以创建一个扩展 BlurDialogFragment 的类,并在 onCreateView 方法中扩展您的自定义布局。请参见下面的示例:

public class CustomDialogFragment extends BlurDialogFragment {


@Override
protected boolean isActionBarBlurred() {
// Enable or disable the blur effect on the action bar.
// Disabled by default.
return true;
}

 @Override
 protected int getBlurRadius() {
// Allow to customize the blur radius factor.
return 7;
}

@Override
protected boolean isDimmingEnable() {
// Enable or disable the dimming effect.
// Disabled by default.
return false;
}


@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                     Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.dialog_fragment_layout, container, 
false);


return v;
}

从您的 Activity 中显示对话框:

FragmentManager fragmentManager = getFragmentManager();
CustomDialogFragment cdf = new CustomDialogFragment();
cdf.show(fragmentManager,"yourTag");

`

关于android - 对话框背后的模糊背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25642472/

相关文章:

java - 在开关按钮中进行套接字编程

android 将图像设置为联系人图标/墙纸

android - 无法从 ActivityGroup 中的 ListActivity 显示 AlertDialog

android - 无法使自定义 DialogFragment 在 Fragment 上透明

android - 如何在警报对话框中为按钮设置可绘制对象?在安卓系统中

android - 检查TextView是否已满

android - 如何注册与 MP3 的新文件类型关联

android - AlertDialog 单元测试 - java.lang.VerifyError : Bad type on operand stack

android - 如何在键盘显示屏上调整 AlertDialog 的大小

android - 如何收听 DialogFragment 关闭事件