android 点击波纹效果
时间: 2023-10-18 17:24:28 浏览: 38
Android 中可以使用 RippleDrawable 来实现点击波纹效果。以下是一个简单的示例:
1. 在 res/drawable 目录下创建一个 ripple_effect.xml 文件,内容如下:
```xml
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/colorPrimaryDark">
<item android:id="@android:id/mask">
<shape android:shape="rectangle">
<solid android:color="#000000" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="@color/colorPrimary" />
</shape>
</item>
</ripple>
```
上述代码定义了一个 RippleDrawable,它包含两个 item。第一个 item 是一个矩形,用来定义 RippleDrawable 的形状,而第二个 item 是一个实心矩形,用来定义 RippleDrawable 的颜色。
2. 将 RippleDrawable 应用到 View 上。例如,你可以在布局文件中如下定义一个 Button:
```xml
<Button
android:id="@+id/my_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me"
android:background="@drawable/ripple_effect" />
```
这样,当用户点击 Button 时,就会出现波纹效果了。
注意:要使用 RippleDrawable,需要在 AndroidManifest.xml 文件中将 minSdkVersion 设为 21 或更高版本。