Boron
Boron

Reputation: 129

Stop AnimatedVectorDrawable animation after playing

I have an AnimatedVectorDrawable that animates from play to pause state when clicked.

...
 animatedVectorDrawable.registerAnimationCallback(new Animatable2.AnimationCallback() {
                    @Override
                    public void onAnimationEnd(Drawable drawable) {
                      //Reset the image back to its original state

                      //What I've tried so far
                      /* img.setImageResource(R.drawable.original_state)
                        animatedVectorDrawable.stop()
                        animatedVectorDrawable.reset() */
                    }
                });
 animatedVectorDrawable.start();

However, I've not been able to successfully get it back to its original state so that I can play it back again. How can I solve this?

Upvotes: 0

Views: 1050

Answers (2)

navylover
navylover

Reputation: 13609

You could do like this:

    final Handler mainHandler = new Handler(Looper.getMainLooper());
    animatedVector.registerAnimationCallback(new Animatable2.AnimationCallback() {
        @Override
        public void onAnimationEnd(final Drawable drawable) {
            mainHandler.post(new Runnable() {
                @Override
                public void run() {
                    animatedVectorDrawable.start();
                }
            });
        }
    });
    animatedVectorDrawable.start();

Upvotes: 0

Arif Rahman
Arif Rahman

Reputation: 67

To get the initial state of the drawable use animatedVectorDrawable.seekTo(0) and the you can call animatedVectorDrawable.stop()

Upvotes: -3

Related Questions