我试图找到一个能够顺利切换AnimationDrawables的解决方案。要做到这一点,我觉得有必要知道AnimationDrawable何时完成动画。我已经看过其他堆栈溢出解决方案,但我找不到适用于oneshot=true
和oneshot=false
动画的解决方案。
此处我的自定义AnimationDrawable:
public class LifecycleAnimationDrawable extends AnimationDrawable {
private boolean finishedAnimating = false;
private AnimationDrawableLifecycleListener animationDrawableLifecycleListener;
public interface AnimationDrawableLifecycleListener {
void onAnimationStart();
void onAnimationFinished();
}
public LifecycleAnimationDrawable(AnimationDrawable drawable, AnimationDrawableLifecycleListener animationDrawableLifecycleListener) {
this.animationDrawableLifecycleListener = animationDrawableLifecycleListener;
for (int i = 0; i < drawable.getNumberOfFrames(); i++) {
addFrame(drawable.getFrame(i), drawable.getDuration(i));
}
setOneShot(drawable.isOneShot());
}
@Override
public void start() {
super.start();
if (animationDrawableLifecycleListener != null) {
animationDrawableLifecycleListener.onAnimationStart();
}
}
@Override
public boolean selectDrawable(int index) {
boolean result = super.selectDrawable(index);
if (index != 0 && index == getNumberOfFrames() - 1) {
if (!finishedAnimating || !isOneShot()) {
finishedAnimating = true;
if (animationDrawableLifecycleListener != null) {
animationDrawableLifecycleListener.onAnimationFinished();
}
}
}
return result;
}
}
我不确定我做错了什么,但我注意到索引似乎总是为0.
任何帮助或见解都将不胜感激。