Android imageview上的涟漪效应
为了描述我的问题,我创建了一个小例子 我有imageview和textview的线性布局。对于linearlayout,我将ripple drawable设置为背景。但当我单击或长时间单击linearlayout ripple时,动画会显示在imageview下。如何在imageview上显示动画 main.xmlAndroid imageview上的涟漪效应,android,rippledrawable,Android,Rippledrawable,为了描述我的问题,我创建了一个小例子 我有imageview和textview的线性布局。对于linearlayout,我将ripple drawable设置为背景。但当我单击或长时间单击linearlayout ripple时,动画会显示在imageview下。如何在imageview上显示动画 main.xml <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayou
<?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>
添加android:background=“@null”
用于ImageView添加如下涟漪
android:foreground="?android:attr/selectableItemBackground"
基于此答案如果你的应用程序需要在API<23上运行,你将无法在除FrameLayout
之外的视图上使用前台
属性,这意味着在视图树层次结构中添加另一个[无用]级别
另一种解决方案是使用
包装图像,将其设置为图像视图的背景,并使用着色和着色模式
来“隐藏”src
图像,使其上有波纹的背景图像可见
不仅此适用于API 21+,而且如果您的图像具有圆角,或者是另一种非矩形形状,如星形或心形图标,涟漪将保持在其边界内,而不是填充视图的矩形边界,这在某些情况下会提供更好的外观
有关动画GIF的信息,请参阅,以查看此技术与使用
或前景属性的比较。在该图像视图中将这两行用作属性
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" />
检查此链接:尝试为图像视图添加android:background=“@null”
。太棒了!这两种解决方案对我都有效。@Ajay,你能发布你的答案吗?我会把它标记为解决方案。我已经添加了答案。这是MinSDK23@netpork不适用于框架布局。对于FrameLayout,前台属性至少在API 16中是可用的,没有问题(在模拟器上测试)。这非常令人印象深刻。你知道如何通过毕加索加载图像来实现这一点吗?@MattMc我认为你必须对
进行子类化,这样当它的src
发生变化时,它也会自动更新其背景
项目的可绘制内容(请参见查看#getBackground()
)。这可能是最容易使用和维护的解决方案。不过我还没有测试。
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
<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" />