Android12 弹窗外部、内部高斯模糊效果实现

目录

一、弹窗内部高斯模糊效果实现:

二、弹窗外部高斯模糊效果实现:

三、弹窗内外均带高斯模糊效果:


一、弹窗内部高斯模糊效果实现:

效果:

只需要在style中声明高斯模糊的属性:

<!--高斯模糊Dialog-->
<style name="BlurDialogTheme" parent="Theme.MaterialComponents.Dialog">
    <item name="android:backgroundDimEnabled">false</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@drawable/dialog_bg</item>
    <item name="android:windowBackgroundBlurRadius" tools:ignore="NewApi">30dp</item>
</style>

backgroundDimEnabled是弹窗外的暗色是否开启,根据实际情况设置即可。

dialog_bg.xml可以声明半径和背景暗色程度:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
        <solid android:color="#2f000000"/>
        <corners android:radius="20dp"/>
</shape>

dialog中使用:

new AlertDialog.Builder(this, R.style.BlurDialogTheme)

二、弹窗外部高斯模糊效果实现:

实现效果:

方案一:

Android12 新增有高斯模糊API:

Android 12 中,可以更轻松地将常用图形效果应用于View上,View中增加了setRenderEffect接口:

public void setRenderEffect(@Nullable RenderEffect renderEffect) {
        ...
}

实现:

所以我们使用这个api实现:

View decorView = getWindow().getDecorView();
decorView.setRenderEffect(RenderEffect.createBlurEffect(25F, 25F, Shader.TileMode.CLAMP));
Dialog dialog = new AlertDialog.Builder(this)
                    .setTitle("test dialog")
                    .setMessage("this is message!")
                    .setPositiveButton("关闭",null)
                    .create();
dialog.setOnDismissListener(dialog1 -> {
         decorView.setRenderEffect(null);
});
dialog.show();

如果是非Android12的话也能实现,需要有最外层的view,获取到当前界面所显示的内容高斯模糊后设置给外层view当背景,本文不做赘述。

实际上,style也为我们提供了弹窗外部高斯模糊的属性,不需要代码来动态设置:

方案二:

style中声明:

    <!--高斯模糊Dialog-->
    <style name="BlurDialogTheme" parent="Theme.MaterialComponents.Dialog">
        <item name="android:windowBlurBehindEnabled" tools:ignore="NewApi">true</item>
        <item name="android:windowBlurBehindRadius" tools:ignore="NewApi">10dp</item>
    </style>

然后dialog中使用:

new AlertDialog.Builder(this, R.style.BlurDialogTheme)

两种方式有一个区别,使用style设置了之后如果背景是动态的,模糊效果依然存在,而使用setRenderEffect的方式则模糊效果会消失。

三、弹窗内外均带高斯模糊效果:

效果:

dialog_bg.xml:左#2f000000,右#2fffffff

 实现:

就是以上两种情况的结合,如果比较懒的话直接用style:

   <!--高斯模糊Dialog-->
    <style name="BlurDialogTheme" parent="Theme.MaterialComponents.Dialog">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@drawable/dialog_bg</item>
        <item name="android:windowBlurBehindEnabled" tools:ignore="NewApi">true</item>
        <item name="android:windowBlurBehindRadius" tools:ignore="NewApi">10dp</item>
        <item name="android:windowBackgroundBlurRadius" tools:ignore="NewApi">30dp</item>
    </style>

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android中可以使用RenderScript库实现高斯模糊效果,同时结合GradientDrawable实现渐变效果。具体实现步骤如下: 1.在build.gradle文件中添加RenderScript支持: ``` defaultConfig { // ... renderscriptTargetApi 19 renderscriptSupportModeEnabled true } ``` 2.创建RenderScript对象: ``` RenderScript rs = RenderScript.create(context); ``` 3.创建高斯模糊脚本: ``` ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); ``` 4.设置高斯模糊半径: ``` blurScript.setRadius(radius); ``` 其中radius是半径值,越大模糊效果越强。 5.将Bitmap转换为RenderScript的Allocation对象: ``` Allocation input = Allocation.createFromBitmap(rs, bitmap); ``` 6.创建一个输出Allocation对象: ``` Allocation output = Allocation.createTyped(rs, input.getType()); ``` 7.在RenderScript中处理图像: ``` blurScript.setInput(input); blurScript.forEach(output); ``` 8.将处理后的图像从Allocation对象中复制到Bitmap中: ``` output.copyTo(bitmap); ``` 9.使用GradientDrawable创建渐变背景: ``` GradientDrawable drawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, new int[] {startColor, endColor}); ``` 其中startColor和endColor是起始和结束颜色。 10.将Bitmap设置为View的背景: ``` view.setBackground(new BitmapDrawable(context.getResources(), bitmap)); ``` 完整的代码示例: ``` RenderScript rs = RenderScript.create(context); ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); blurScript.setRadius(radius); Allocation input = Allocation.createFromBitmap(rs, bitmap); Allocation output = Allocation.createTyped(rs, input.getType()); blurScript.setInput(input); blurScript.forEach(output); output.copyTo(bitmap); GradientDrawable drawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, new int[] {startColor, endColor}); view.setBackground(new BitmapDrawable(context.getResources(), bitmap)); ``` 注意:以上代码仅为示例,具体实现可能需要根据项目需求进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值