【问题标题】:RippleDrawable NOT drawing over ViewsRippleDrawable 不绘制视图
【发布时间】:2015-07-27 12:32:18
【问题描述】:

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

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

这是我的布局:

<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 时,它曾经完美地工作?

【问题讨论】:

    标签: android android-layout android-5.0-lollipop material-design


    【解决方案1】:

    无限的涟漪被投射到祖先视图的第一个可用背景上。从托管波纹的ImageView 跟踪视图层次结构,您会发现第一个可用背景位于LinearLayout 上,标识符为drawer

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

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

    【讨论】:

    • 谢谢!这是我能够找到的第一个答案,它准确地解释了为什么会发生这种情况。将我的图像包裹在 FrameLayout 中并将其背景设置为透明色,使波纹再次可见。
    【解决方案2】:

    从您自己的代码 ('setBackground') 中可以看出,您将波纹设置为 BACKGROUND,这就是它被绘制在背景上的原因。

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

    您的代码本身没有任何问题。但是对于 3rd 方库,Android 团队无法保证此类行为。

    这里有几个选项会因清洁度、工作量和性能而异:

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

    【讨论】:

    • 我想说的解释很精彩。我遵循了第二个解决方案,它奏效了。现在有点问题.. 以前使用 setBackground() 时,波纹的形状是圆形的(即使我的 ImageView 是矩形的),但现在使用 setForeground() 波纹不是方形的(我的意思是边界)。
    • 不确定。也许与 android:clipChildren="false" android:clipToPadding="false" 有关,也许您应该在布局中的某处将它们设置为 true,以允许波纹在 FrameLayout 边界之外绘制
    • android:drawSelectorOnTop 不是ImageViewripple 的有效属性。你们是从哪里得到这些信息的?
    • @alanv外星人,一定是外星人发来的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多