imageview 的涟漪效应

Ripple effect over imageview

为了描述我的问题,我创建了一个小例子。

我有带图像视图和文本视图的线性布局。对于 linearlayout,我将 ripple drawable 设置为背景。但是当我点击或长按 linearlayout 波纹动画显示在 imageview 下。如何在 imageview 上显示动画?

main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/linear"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="@drawable/ripple"
        android:clickable="true"
        android:orientation="vertical">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:src="@mipmap/index" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="This is ripple test"
            android:textColor="#FF00FF00" />
    </LinearLayout>

</android.support.constraint.ConstraintLayout>

drawable-v21/ripple.xml:

<?xml version="1.0" encoding="utf-8"?>
<ripple
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#FFFF0000">   
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#FF000000"/>
        </shape>
    </item>    
</ripple>

drawable/ripple.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">    
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <corners android:radius="3dp" />
            <solid android:color="#FFFF0000" />
        </shape>
    </item>
    <item android:state_focused="true">
        <shape android:shape="rectangle">
            <corners android:radius="3dp" />
            <solid android:color="#FFFF0000" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <corners android:radius="3dp" />
            <solid android:color="#FF000000" />
        </shape>
    </item>    
</selector>

现在的截图:

ImageView

添加 android:background="@null"

像这样添加波纹

android:foreground="?android:attr/selectableItemBackground"

基于这个答案

如果您的应用需要 运行 API < 23,您将无法在 FrameLayout 以外的视图上使用 foreground 属性,这意味着在视图树层次结构中添加另一个[无用]级别。

另一个解决方案是用 <ripple> 包装您的图像,将其设置为您的 ImageViewbackground,然后 使用 tinttintMode 到 "hide" src 图像,这样可以看到上面有波纹的背景图像。

<!-- In your layout file -->

<ImageView
    android:adjustViewBounds="true"
    android:background="@drawable/image_with_ripple"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:src="@drawable/image"
    android:tint="@android:color/transparent"
    android:tintMode="src_in" />
<!-- drawable/image_with_ripple.xml -->

<?xml version="1.0" encoding="utf-8"?>
<ripple
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?colorControlHighlight">

    <item android:drawable="@drawable/image" />

</ripple>

不仅这个 适用于 API 21+,而且如果您的图像有圆角 – 或者是另一种非矩形形状,如星形或心形图标 – 波纹将保留在其边界内,而不是填充视图的矩形边界,这在某些情况下会提供更好的外观。

查看 this Medium article 动画 GIF,了解此技术与使用 <FrameLayout>foreground 属性相比如何。

简单地将这两行用作该 ImageView 中的属性。

android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"

解决 API < 21

 <ImageView
            android:id="@+id/favorite_season"
            style="?android:attr/borderlessButtonStyle"
            android:background="?android:attr/selectableItemBackground"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_margin="22dp"
            android:clickable="true"
            android:focusable="true"
            android:src="@drawable/ic_star"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent" />