Android 高斯模糊(1) 窗口模糊及java侧基本流程简述

Android 12起,原生支持了窗口模糊接口能力。

Window blurs | Android Open Source Project

按照官方的描述分为以下两种

  1. blur behind :使窗口后面的整个屏幕变得模糊

  2. background blur :使指定区域的底层内容模糊

其本质都是对当前surface后面的surface进行模糊显示。

blur behind

公开接口可通过主题配置或者代码设置,两种方式任选其一

xml 复制代码
<style name="BlurBehind" parent="Theme.CarLauncher">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBlurBehindEnabled">true</item>
    <item name="android:windowBlurBehindRadius">70dp</item>
</style>
kotlin 复制代码
// 代码方式设置
window.addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND)
window.attributes = window.attributes.apply {
 blurBehindRadius = 70
} 

blurBehindRadius参数会传递到system_server端 WindowState中,由Dimmer设置(新建了一个dimLayer),最终是调用

setBackgroundBlurRadius 接口

background blur

xml 复制代码
<style name="BackgroundBlur" parent="Theme.CarLauncher">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackgroundBlurRadius">70dp</item>
</style>
kotlin 复制代码
// 代码方式设置
 window.setBackgroundBlurRadius(70)

setBackgroundBlurRadius时做了什么事?

仅仅是创建了一个BackgroundBlurDrawable并设置到DecorView的背景中

BackgroundBlurDrawable

BackgroundBlurDrawable到底做了什么神奇的事情实现了背景高斯模糊?

BackgroundBlurDrawable代码只有短短几百行,相对比较简单,

核心只是:记录下模糊参数:坐标范围,圆角值,模糊半径,透明度。

圆角值,模糊半径,透明度都可以通过BackgroundBlurDrawable公开接口设置,不多解释。

坐标范围是通过监听RenderNode位置变化获得

Aggregator可以保存多个BackgroundBlurDrawable,也就是说:

一个窗口上是可以使用多个BackgroundBlurDrawable

一个ViewRootImpl对应一个Aggregator,Aggregator对应多个BackgroundBlurDrawable

Aggregator如何连通ViewRootImpl和BackgroundBlurDrawable

每一次刷新界面时,如果Aggregator模糊区域发生更新 或者存在模糊区域,

会读取当前的模糊区域参数,发送给SurfaceFlinger端

每个BackgroundBlurDrawable都可以转换成一个BlurRegion数据结构,

所以多个Drawable就转换成了一个BlurRegion数组

一个BlurRegion可以转换为一个float数组,那么多个BlurRegion可转换为一个float二维数组

即 多个BackgroundBlurDrawable记录的模糊参数,最终转换成了float二维数组

然后将表示模糊参数的float二维数组通过setBlurRegions下设

有一个细节是:监听模糊坐标范围mRect更新并没有立即执行,而是作为Runable和帧数一起记下,在getBlurRegionsToDispatchToSf时再真正执行,这样避免了多线程同步问题。

如果mRect立即更新,该操作在线程池中执行,无法保证执行顺序。假如监听到第100帧的RenderNoder位置变化mRect立即更新了,但是renderThread此时正在请求渲染第99帧,却使用了第100帧的位置,就会出现位置错位的问题。

总结

综上,blur behind 本质上是使用了setBackgroundBlurRadius , background blur本质上是使用了setBlurRegions。

那么利用上面的接口,就可以实现surface的实时模糊显示。

比如:在一个surfaceView中显示视频或者3D画面,上面的surface使用上面的接口就可以实现模糊效果。

相关推荐
FungLeo2 小时前
FengCMS · PHP+MySQL 升级实录 · 危险默认值之殇:root/123456、认证码 8888 写进配置加载器
android·mysql·php·安全加固·fengcms
yz_aiks2 小时前
Termux 使用教程:把安卓手机变成随身 Linux(装 Python、SSH 远程、跑自动化脚本)
android·linux·python·智能手机·ssh·termux
booboosui10 小时前
编号没变,消息还是来了:Mac 端通知读取漏第二条的原因
android
雨白1 天前
单例设计模式:从 Java/Kotlin 实现到 Android 实战
android
hai_android1 天前
一行 asLiveData,背后是一座生命周期的桥
android·java·kotlin
阿杰m1 天前
Android源码第二十三章:Telephony、RIL 与 IMS——来电怎么到应用?
android
booboosui1 天前
没人认领 .md:安卓把报告交给了音乐播放器打开
android
用户60709759149061 天前
AOSP 14 OTA 升级指导(四):update_engine 内部机制——Action 流水线与写盘
android
恋猫de小郭2 天前
Jetpack Compose 1.13 发布,憋了这么久终有交代了
android·前端·flutter
JMchen1232 天前
【Android 性能优化实战 60 讲】12 异步 Inflate 实战与痛击:源码拆解原生瓶颈,手写增强版布局预加载框架
android·性能优化·实战·启动优化·异步inflate·x2c