android - Android 12 RenderEffect 模糊不是应用于背景而是应用于整个视图
问题描述
我正在尝试使用新的 Android 12 RenderEffect API 来模糊底页。
但是直到现在我可以在整个视图上获得模糊效果,使文本无法阅读。
现在,这是我的底部工作表布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:orientation="vertical">
<Space
android:layout_width="1dp"
android:layout_height="6dp"/>
<View
android:id="@+id/smallButtonRect"
android:layout_width="24dp"
android:layout_height="4dp"
android:layout_gravity="center"
android:background="@drawable/bg_bottom_line"/>
<Space
android:layout_width="1dp"
android:layout_height="14dp"/>
<!-- navigation header layout -->
<RelativeLayout
android:id="@+id/header_layout"
android:layout_width="match_parent"
android:paddingBottom="10dp"
android:layout_height="wrap_content">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="22sp"
android:text="@string/data_saver_title"
android:fontFamily="@font/manrope"
android:textColor="@color/colorTextPrimary" />
</RelativeLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_gravity="center">
<TextView
android:id="@+id/subtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="15sp"
android:fontFamily="@font/manrope"
android:text="@string/data_saver_description"
android:textColor="@color/colorTextSecondary" />
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical"
android:gravity="center"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:paddingBottom="@dimen/card_padding">
<com.google.android.material.button.MaterialButton
android:id="@+id/data_saver_enabled"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@android:color/transparent"
android:fontFamily="@font/manrope"
android:stateListAnimator="@null"
app:icon="@drawable/ic_data_on"
android:text="@string/data_saver_enabled"
android:textAllCaps="false"
app:cornerRadius="@dimen/button_round" />
</LinearLayout>
虽然这是我创建底部工作表的方式:
final View bottomNav = getLayoutInflater().inflate(R.layout.bottom_sheet_data_saver,null);
bottomSheetData = new BottomSheetDialog(MainActivity.this, R.style.BottomSheetDialogTheme);
bottomSheetData.setContentView(bottomNav);
bottomSheetData.setCancelable(true);
bottomSheetData.show();
在这里,我如何使用新 API 应用模糊:
((View) bottomNav.getParent()).setRenderEffect(RenderEffect.createBlurEffect(10f, 10f, Shader.TileMode.CLAMP));
我究竟做错了什么?我该如何做才能仅在背景中应用模糊,从而使底部页面模糊到较低的覆盖内容?
更新:根据用户的建议,我已经尝试过了。我没有在底片上变得模糊。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
bottomSheetData.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
bottomSheetData.getWindow().getAttributes().setBlurBehindRadius(10);
}
解决方案
在调用 BottomSheetDialog.show() 之前,您需要设置设置窗口标志和属性。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
dialog.window!!.addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND)
dialog.window!!.attributes.blurBehindRadius = blurRadius
}
你会看到对话框的外部背景是模糊的。
推荐阅读
- python - numpy:沿两个轴的矢量化累积和
- azure-functions - 在 Docker 容器上的 Azure Function Apps Runtime 2.x 中运行 csproj 函数时,如何根据环境条件设置 AuthorzationLevel?
- spring - 是否可以在 spring elasticsearch 中使用 _update_by_query
- html - 在字段中输入文本时更改提交按钮的颜色
- vba - 如何用零值声明窗口
- jquery - jQuery ScrollTop 到元素 ID 在 Safari 上不起作用
- python-3.x - Flask-AppBuilder 等效于 SQLite WHERE 子句来过滤列数据
- ruby-on-rails - Ruby on Rails 保存模型 has_many 到 has_many 不会在构建时触发
- sql - SQL query to combine column data from multiple columns
- typescript - 基于动态(映射/条件)类型获取类型完成