Primero cree un archivo dibujable y agregue un archivo de recursos
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@color/rate_01"
android:duration="16"/>
<item android:drawable="@color/rate_02"
android:duration="16"/>
<item android:drawable="@color/rate_03"
android:duration="16"/>
</animation-list>
opciones de diseño
<ImageView
android:id="@+id/my_anim"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/my_anima"
/>
Animación de control en código
AnimationDrawable drawable = (AnimationDrawable) binding.myAniView.getDrawable();
drawable.start();
drawable.stop();
Iniciar animación
Pausa
Escuche el final de la animación y cambie a la siguiente animación. AnimationDrawable en sí no tiene monitoreo final de animación, puede personalizar AnimationDrawable
public class AnimationDrawableLife extends AnimationDrawable {
private AnimationEndListener listener;
AnimationDrawableLife(AnimationEndListener listener) {
this.listener = listener;
}
@Override
public boolean selectDrawable(int index) {
if (index != 0 && index == getNumberOfFrames() - 1) {
if (listener != null) listener.onAnimationEnd();
}
return super.selectDrawable(index);
}
interface AnimationEndListener {
void onAnimationEnd();
}
}
usar
AnimationDrawableLife animationDrawableLife = new AnimationDrawableLife(new AnimationDrawableLife.AnimationEndListener() {
@Override
public void onAnimationEnd() {
binding.myAniView.setImageDrawable(animationDrawable);
animationDrawable.start();
}
});
binding.myAniView.setBackground(animationDrawableLife);
animationDrawableLife.start();