gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 10:19:49 26 4
gpt4 key购买 nike

我想要对话框下面有模糊的屏幕,所以我拍摄了 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com