I need a dialog with a blurry screen below it, so I take a “screenshot” of the activity, blur it and set it as the background of the dialog box as BitmapDrawable. The strange thing is that the dialogue is no longer focused on the screen, and the external dialogue is not closed, even if setCanceledOnTouchOutside (true) was called.
Question: why is this not working? Accordingly, how to create a dialogue with a blurred background?
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);
RenderScript rs = RenderScript.create(getActivity());
final Allocation input = Allocation.createFromBitmap(rs, b);
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;
}
}

source
share