0

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.

isuPatches
  • 6,384
  • 2
  • 22
  • 30
  • refer this. https://github.com/metova/metova-android-sdk/blob/master/metova-android-core/src/com/metova/android/graphics/drawable/CustomAnimationDrawable.java – Radhey Sep 05 '17 at 05:06
  • @Radhey I have attempted to use that implementation. It seems that with all of the implementations, as I stated, int index for selectDrawable is always 0 so it never seems to progress. – isuPatches Sep 05 '17 at 10:47
  • What you are trying to do or acheive exactly with AnimationDrawable! ,If you want to forcefully stop animation of AnimationDrawable then use ((AnimationDrawable)(someButton.getBackground())).stop(); – Radhey Sep 05 '17 at 10:58
  • refer this also ,https://stackoverflow.com/a/6641321/1848157 – Radhey Sep 05 '17 at 11:00
  • @Radhey, I just need to hold off the animation until the current animation completes. I have also looked at that other post, nothing so far has worked because of the issue of index always being 0, animation never gets registered as finished. – isuPatches Sep 05 '17 at 11:04
  • you are applying this custom animation in multiple views simultaneously! – Radhey Sep 05 '17 at 11:08
  • I am not...just one view – isuPatches Sep 05 '17 at 11:10
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/153678/discussion-between-radhey-and-isupatches). – Radhey Sep 05 '17 at 11:10

0 Answers0