【问题标题】:Dim and Blur Background Behind Fragment AndroidFragment Android 背后的暗淡和模糊背景
【发布时间】:2016-03-13 21:17:40
【问题描述】:

所以我有一个自定义的“对话框”,它实际上只是框架布局中的一个片段。 无论如何,我希望它一旦显示就模糊和变暗它背后的任何东西。 我目前正在使用这个,在 onCreateView

  // Dim and blur background
    getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);

但是,这并没有使包含 RecyclerView 和 CardView 的其他框架布局模糊或变暗,这些元素仍然以完全不透明的方式显示,不知道为什么?

这是一个截图,您可以看到背景变得模糊,但并非片段后面的所有视图都变暗

我希望能够实现 NavigationDrawer 在您打开它时的外观,这是同一个应用程序中的一个示例,所有内容都被正确调暗和模糊。

这是我的 XML 文件,以防万一,

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".controllers.MainActivity"
android:id="@+id/app_bar_main_layout">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

 // This is the framelayout for a fragment
 // The fragment contains a RecyclerView and a CardView
 // This is what I would like to be dimmed.
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/frame_layout_container"
    android:background="@color/colorCardDisplayBackground"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">
</FrameLayout>


 // This is my custom "Dialog" fragment
<FrameLayout
    android:elevation="6dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:id="@+id/card_fragment_container">

    <ImageButton
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:layout_gravity="bottom|end"
        android:layout_marginRight="15dp"
        android:background="@drawable/round_button_background"
        android:elevation="8dp"
        android:src="@drawable/ic_done_white_24dp"
        android:scaleType="center"
        android:visibility="gone"
        android:id="@+id/final_confirm_card_button"/>
</FrameLayout>

<com.getbase.floatingactionbutton.FloatingActionButton
    android:layout_width="wrap_content"
    android:id="@+id/mainFab"
    android:layout_height="wrap_content"
    fab:fab_icon="@drawable/ic_add_white_48dp"
    fab:fab_colorNormal="@color/colorAccent"
    fab:fab_colorPressed="@color/colorAccentLight"
    android:layout_gravity="bottom|end" />

【问题讨论】:

  • @DanielNugent,这不是一个可能的解决方案,实际上也不能解决这个问题。
  • 事实上,一个 DialogFragment 是 Android 框架提供的,以便轻松地完成您想要做的事情。您可以在 DialogFragment 中实现与在任何其他 Fragment 中相同的功能,并且背景将以标准方式变暗和模糊。如果您的项目架构阻止您使用 DialogFragment,那就这样吧,但如果您可以使用它,它将解决您的问题。

标签: java android android-studio


【解决方案1】:

如评论中所述,FLAG_BLUR_BEHIND 不适用于我使用的框架布局。

我决定只更改父级的前景

// Dim the background
// Where mainFrameLayout is as FrameLayout
mainFrameLayout.getForeground().setAlpha(220);

当你想消除“暗淡”效果时

// Remove dim background color
mainFrameLayout.getForeground().setAlpha(0);

【讨论】:

    【解决方案2】:

    FLAG_BLUR_BEHIND 标志自 API 14 以来已被弃用,因此您不应期望它能够工作。

    很遗憾,Android 在弃用此标志时并未提供内置替代方案。但是,已经开发了几种替代解决方案。我建议您查看here 中描述的 500px 模糊库。

    此视图背后的想法是您调用.setBlurredView(View view)view 是要模糊的BlurringView 下面的视图。然后,每当要重新绘制模糊时,调用.invalidate(),因为要模糊的视图发生了变化。请注意,这种模糊效果利用了RenderScript ScriptIntrinsicBlur 效果。

    【讨论】:

    • 会看看这个,因为它看起来很有趣,但是 DIM_BEHIND 并没有贬值,它仍然没有使我的片段后面的视图变暗,这很奇怪?
    • 我以前没有使用过那个标志,但是通过上面描述的方法,你也可以应用一个暗淡。 RenderScript ScriptIntrinsicBlur 采用 Bitmap 并返回模糊的 Bitmap。然后,您可以从模糊的位图制作一个新的位图,每个像素都会变暗一定量。
    • 刚刚看了你提到的库,根据 github,它不能与 RecyclerViews 一起工作,我无法让它工作,很遗憾,它不会为我修复它。
    • 如果我不使用 RecyclerView,这个解决方案会为我工作,我为我的问题修复它的方式是添加一个前景可绘制对象(黑色)并将 alpha 更改为 > 0 每当我想要调暗那个特定的视图。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-15
    • 2017-09-19
    • 1970-01-01
    • 2013-10-19
    • 2011-03-14
    • 2015-10-26
    • 1970-01-01
    相关资源
    最近更新 更多