android button背景光晕动画
时间: 2023-07-21 11:41:51 浏览: 54
要实现 Android Button 背景光晕动画可以使用 Android 中的 RippleDrawable。RippleDrawable 是一个可以在点击时产生涟漪效果的 Drawable,可以作为 View 的背景。下面是一个简单的实现步骤:
1. 在 res/drawable 文件夹下创建一个 xml 文件,例如 button_bg.xml,用来定义 RippleDrawable。
```xml
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?attr/colorControlHighlight">
<item android:id="@android:id/mask">
<shape android:shape="rectangle">
<solid android:color="@color/white" />
</shape>
</item>
<item android:id="@android:id/background">
<shape android:shape="oval">
<solid android:color="@color/white" />
</shape>
</item>
</ripple>
```
2. 在 Button 的布局文件中设置背景为 button_bg.xml。
```xml
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:background="@drawable/button_bg" />
```
3. 在 Button 的 Java 代码中设置点击事件,以及启动 RippleDrawable 动画。
```java
Button button = findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RippleDrawable rippleDrawable = (RippleDrawable) v.getBackground();
rippleDrawable.setHotspot(v.getWidth() / 2, v.getHeight() / 2);
rippleDrawable.setVisible(true, true);
}
});
```
这样,在点击 Button 时就会出现一个背景光晕动画。你可以根据需要调整 button_bg.xml 中的参数来改变动画的效果。