Determining finish for oneshot and non-oneshot AnimationDrawables

67 views Asked by At

I'm trying to find a solution for switching between AnimationDrawables smoothly. To do this, I feel it's necessary to know when the AnimationDrawable is finished animating. I've seen other stack overflow solutions, but I have not found one that works for both oneshot=true and oneshot=false animations.

Here's my custom AnimationDrawable at this time:

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;
    }
}

I'm not sure what I'm doing wrong, but I have noticed that the index seems to always be 0.

Any help or insight would be appreciated.

0

There are 0 answers