使用selectableItemBackgroundBorderless,您可以这样做:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/nav_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/holo_green_light"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:scaleType="fitXY"
android:src="@drawable/device_bg" />
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:src="@drawable/sidebar_refresh" />
<LinearLayout
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true">
<FrameLayout
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="7dp"
android:background="@android:color/transparent">
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:src="@drawable/sidebar_refresh"
android:alpha="0"
android:background="?attr/selectableItemBackgroundBorderless"/>
</FrameLayout>
</LinearLayout>
</RelativeLayout>
关键是波纹效果必须有自己的布局(LinearLayout+FrameLayout),然后使用alpha="0"将有波纹的图像设置为透明,以便覆盖实心的图像。
这段代码的问题是:
- 您必须对填充进行硬编码。如果没有填充或填充太小,波纹效果将绑定为矩形,而不是圆形。使用公式 48(height/width)/7 计算填充。如果除以 7 不起作用,请减小除数,例如6.
- 因为它有填充,所以你不能触摸填充来触发波纹效果。如果这是一个小按钮是可以接受的,因为它不明显,但如果它是一个大按钮则很重要。
好消息是在drawable/ripple.xml 中使用ripple 标签模拟selectableItemBackgroundBorderless 有另一种方法:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorPrimary">
<item android:id="@android:id/mask">
<shape android:shape="oval">
<solid android:color="?android:colorPrimary"/>
</shape>
</item>
</ripple>
据我观察,它给出了与selectableItemBackgroundBorderless 一样的涟漪效应(除了它没有溢出涟漪),所以你没有理由坚持使用默认的selectableItemBackgroundBorderless
有了这个ripple.xml,这样做:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/nav_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/holo_green_light"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:scaleType="fitXY"
android:src="@drawable/device_bg" />
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:src="@drawable/sidebar_refresh" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true">
<FrameLayout
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@android:color/transparent">
<ImageView
android:id="@+id/imageView3"
android:layout_width="48dp"
android:layout_height="48dp"
android:clickable="true"
android:src="@drawable/ripple" />
</FrameLayout>
</LinearLayout>
</RelativeLayout>
唯一改变的是LinearLayout 部分。没有更多的硬编码填充和 alpha 图像。唯一的小问题是点击角落仍然会触发连锁反应。我找到了这个thread,但我还没有尝试过。
即使图像不透明,上述两种方法也有效。