RippleDrawable 不在视图上绘制

RippleDrawable NOT drawing over Views

我有一个布局,上面有一个 KenBurnsView 和一个 ImageView(只是一个切换按钮)。当我单击按钮时,会生成一个 Ripple,但绘制在 KenBurnsView 下方。

以前,当我用图像视图代替 KenBurnsView 时,波纹绘制在顶部的图像视图上方。

这是我的布局:

<LinearLayout 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"
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background"
    android:clickable="true"
    android:orientation="vertical">


    <RelativeLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="@dimen/nav_drawer_header_height">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <com.flaviofaria.kenburnsview.KenBurnsView
                android:id="@+id/header_cover"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/cover_1" />

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ImageView
                    android:id="@+id/header_toggle"
                    android:layout_width="50dp"
                    android:layout_height="30dp"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentRight="true"
                    android:layout_marginBottom="10dp"
                    android:layout_marginRight="10dp"
                    android:padding="10dp"
                    android:src="@drawable/toggle_down" />

            </RelativeLayout>

        </FrameLayout>

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/nav_toggle_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></RelativeLayout>

</LinearLayout>

这是我的涟漪图 XML:

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@android:color/white"
    android:drawSelectorOnTop="true"> <!-- ripple color -->

</ripple>

这就是我添加波纹的方式:

toggle.setBackground(getResources().getDrawable(R.drawable.ripple));

Ripple 在 KenBurnsView 下方得到 drwan 的问题是什么?当有一个 ImageView 代替 KenBurnsView 时,它曾经完美地工作?

正如您从自己的代码 ('setBackground') 中看到的那样,您将涟漪设置为 BACKGROUND 这就是它被绘制在背景上的原因。

android API 上的 ImageView 21 添加了这个 "hack" 用于波纹 android:drawSelectorOnTop="true"。但是您使用的库没有添加相同的 hack。

您的代码本身没有任何问题。但是第 3 方库的 Android 团队无法保证这种行为。

这里有几个选项,它们在清洁度、工作量和性能方面会有所不同:

  1. 检查 ImageView 源代码,克隆库,添加 imageview 用于涟漪的相同 hack。正常工作后,确保将请求拉回库。
  2. 用 FrameLayout 包裹 KenBurnsView 并使用 FrameLayout 上的 setForeground 设置波纹。
  3. 克隆库,添加前景可绘制选项(类似于此 How to set foreground attribute to other non FrameLayout view)。还要确保将这个有价值的代码请求回库。

无限涟漪投射到祖先视图的第一个可用背景上。从承载涟漪的 ImageView 向上追溯视图层次结构,您会发现第一个可用背景位于 LinearLayout 上,标识符为 drawer.

如果您在包含您的 ImageViewRelativeLayout 上设置透明背景,第一个可用背景将呈现在同级视图上方,您将获得所需的效果。

旁注,RelativeLayout 应替换为 FrameLayout,这将通过更便宜的布局传递提供相同的效果。