先看效果:
有点糊,不过效果还是能看到的。
步骤:
1:在你的界面布局最下面加入这个(哪个界面要弹窗就在哪个界面xml加)
<!-- 这里的FrameLayout用于承载对话框的背景 -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iv_dialog_bg"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
2:添加透明背景
<item name="android:windowTranslucentStatus">true</item>
如图:
3:工具类先贴出来吧,主要代码都抽到工具类了
/**
* @Description: java类作用描述
* @author: 小张
* @date: 2023/2/10
*/
public class BlurryBgUtil {
private static int originalW;
private static int originalH;
private static Bitmap captureScreen(Activity activity) {
activity.getWindow().getDecorView().destroyDrawingCache(); //先清理屏幕绘制缓存(重要)
activity.getWindow().getDecorView().setDrawingCacheEnabled(true);
Bitmap bmp = activity.getWindow().getDecorView().getDrawingCache();
//获取原图尺寸
originalW = bmp.getWidth()+10;
originalH = bmp.getHeight();
//对原图进行缩小,提高下一步高斯模糊的效率
bmp = Bitmap.createScaledBitmap(bmp, originalW / 2, originalH / 2, false);
return bmp;
}
public static void setScreenBgLight(Activity context) {
Window window = context.getWindow();
WindowManager.LayoutParams lp;
if (window != null) {
lp = window.getAttributes();
lp.dimAmount = 0.1f;
window.setAttributes(lp);
}
}
public static void handleBlur(Activity context, ImageView dialogBg,Handler mHandler) {
Bitmap bp = captureScreen(context);
bp = blur(bp,context); //对屏幕截图模糊处理
//将模糊处理后的图恢复到原图尺寸并显示出来
bp = Bitmap.createScaledBitmap(bp, originalW, originalH, false);
dialogBg.setImageBitmap(bp);
dialogBg.setVisibility(View.VISIBLE);
//防止UI线程阻塞,在子线程中让背景实现淡入效果
asyncRefresh(true,dialogBg,mHandler);
}
public static void asyncRefresh(boolean in,ImageView dialogBg,Handler mHandler) {
//淡出淡入效果的实现
if(in) { //淡入效果
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 256; i += 5) {
refreshUI(i,dialogBg);//在UI线程刷新视图
try {
Thread.sleep(4);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
} else { //淡出效果
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 255; i >= 0; i -= 5) {
refreshUI(i,dialogBg);//在UI线程刷新视图
try {
Thread.sleep(4);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//当淡出效果完毕后发送消息给mHandler把对话框背景设为不可见
mHandler.sendEmptyMessage(0);
}
}).start();
}
}
public static void refreshUI(final int i, ImageView dialogBg) {
runOnUiThread(new Runnable() {
@Override
public void run() {
dialogBg.setImageAlpha(i);
}
});
}
public static void hideBlur(ImageView dialogBg,Handler mHandler) {
//把对话框背景隐藏
asyncRefresh(false,dialogBg,mHandler);
System.gc();
}
public static Bitmap blur(Bitmap bitmap,Activity activity) {
//使用RenderScript对图片进行高斯模糊处理
Bitmap output = Bitmap.createBitmap(bitmap); // 创建输出图片
RenderScript rs = RenderScript.create(activity); // 构建一个RenderScript对象
ScriptIntrinsicBlur gaussianBlue = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); //
// 创建高斯模糊脚本
Allocation allIn = Allocation.createFromBitmap(rs, bitmap); // 开辟输入内存
Allocation allOut = Allocation.createFromBitmap(rs, output); // 开辟输出内存
float radius = 10f; //设置模糊半径
gaussianBlue.setRadius(radius); // 设置模糊半径,范围0f<radius<=25f
gaussianBlue.setInput(allIn); // 设置输入内存
gaussianBlue.forEach(allOut); // 模糊编码,并将内存填入输出内存
allOut.copyTo(output); // 将输出内存编码为Bitmap,图片大小必须注意
rs.destroy();
//rs.releaseAllContexts(); // 关闭RenderScript对象,API>=23则使用rs.releaseAllContexts()
return output;
}
}
4:在需要弹窗的界面中:initView中
private ImageView dialogBg;
private Handler mHandler;
*****
之后获取 刚刚布局里的哪个imageview
//模糊背景
dialogBg = findViewById(R.id.iv_dialog_bg);
//创建activity先把对话框背景图设为不可见
dialogBg.setImageAlpha(0);
dialogBg.setVisibility(View.GONE);
//hanlder初始化
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 0) {
dialogBg.setVisibility(View.GONE);
}
}
};
5:点击弹窗触发时:
然后在点击弹窗触发的时候:
BlurryBgUtil.handleBlur(Linkman_Activity.this,dialogBg,mHandler);
如图:
6:popup点击取消 确认触发
// 背景淡出
BlurryBgUtil.hideBlur(dialogBg,mHandler);
如图:
7:shift + F10 (运行)
完活。就这么多
又是被美工逼着进步的一天啊~~~.