Android 12 RenderEffect 模糊不是应用于背景而是应用于整个视图

问题描述 投票:0回答:2

我正在尝试使用新的 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);
    }
android android-studio android-layout render blur
2个回答
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
}

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


0
投票

使用 BottomSheetDialogFragment 并在 onViewCreated 方法中使用这些:

Window window = getDialog().getWindow();
         window.addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
         window.getAttributes().setBlurBehindRadius(10);
         window.setAttributes(window.getAttributes()); 

希望它可以帮助你;)enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.