// 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;
}
5条答案
按热度按时间nkhmeac61#
更新:
我们还可以使用
renderscript
API来模糊API级别〉= 17的背景,并使用fastblur来模糊API级别〈17的背景。下面是Github项目。- Github Project
- Blog
使用FastBlur的步骤
1)使用下面的代码拍摄您的背景快照
这样使用
Bitmap map=takeScreenShot(BlurImageView.this);
//您的活动名称2)调用方法
Bitmap fast=fastblur(map, 10);
从Here获取3)最后在
Button
上单击That's It Now You can see the Blur Image Behind You活动
希望这能对任何人都有帮助…
bzzcjhmw2#
这个来自nitesh的解决方案的工作方式如下
但如果你不能让你的警报到中心(像我一样),也检查我的答案,这是Here
我所做的是
使用了两个
alertDialogs
一个是模糊效果(使用getWindow()
),现在没有显示(我删除了它的setContentView
/可以使用透明背景)另一个是真实的的
alertDialog
在模糊效果后得到的显示输出
lnxxn5zx3#
对于快速模糊和解决对话框出现在屏幕顶部的问题,我认为有一个更简单的解决方案:
模糊方法:
TakeScreenShot方法:
GetScreenSize方法:
要使用RenderScript(To blur),必须在App的build.gradle中添加:
bqf10yzr4#
试试这个
blpfk2vs5#