在对话框后面的模糊

时间:2014-09-03 10:53:08

标签: android android-alertdialog android-dialogfragment

我希望对话框下面有模糊的屏幕,所以我采取了#34;截图"活动,模糊它并将对话框窗口的背景设置为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

2 个答案:

答案 0 :(得分:1)

这篇文章很旧,但是供您参考:

要创建背景模糊的对话框,您可以使用此库:

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;
}

显示您的活动对话框:

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

`

答案 1 :(得分:1)

请检查:https://stackoverflow.com/a/21278278/3891036

它适用于我。

OR

创建 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>

然后在你的对话框上

dialog = new Dialog(context,styles);