【问题标题】:Android 12 RenderEffect blur is not applied to the background but to the entire viewAndroid 12 RenderEffect 模糊不是应用于背景而是应用于整个视图
【发布时间】:2021-10-27 22:43:47
【问题描述】:

我正在尝试使用新的 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);
    }

【问题讨论】:

  • 新 API 适用于您对其及其所有子项标识的视图。如果您只是想要一个模糊的背景,您可以定义一个覆盖底部工作表但位于所有其他子级之后的视图,并将新的 API 应用到它。

标签: android android-studio android-layout render blur


【解决方案1】:

在调用 BottomSheetDialog.show() 之前,需要设置设置窗口标志和属性。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
    dialog.window!!.addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND)
    dialog.window!!.attributes.blurBehindRadius = blurRadius
}

你会看到对话框外面的背景是模糊的。

【讨论】:

  • 方法 blurBehindRadius 不存在。
  • 你是认真的吗?如果找不到方法,请检查编译sdk版本或更新您的android sdk,好吗?
  • 这里是sdk链接setBlurBehindRadius
  • 我已经更新了我的问题,你的建议不起作用。
  • 任何示例来解释你想要什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-15
  • 1970-01-01
  • 1970-01-01
  • 2013-12-23
相关资源
最近更新 更多