Anton Holovin
Anton Holovin

Reputation: 5673

Reverse animation in AnimatedVectorDrawable

Is that possible to play the animation in reverse order for AnimatedVectorDrawable?

Upvotes: 8

Views: 2416

Answers (1)

Mikhail
Mikhail

Reputation: 294

If you look into AnimatedVectorDrawable source code, you will find the method

/**
 * Reverses ongoing animations or starts pending animations in reverse.
 * <p>
 * NOTE: Only works if all animations support reverse. Otherwise, this will
 * do nothing.
 * @hide
 */
public void reverse()

You can call this method using reflection. For example like this:

    private boolean mAnimationReversed = false;

    public void startAnimation() {
        if(mAnimationReversed) {
            try {
                Method reverse = mDrawable.getClass().getMethod("reverse");
                reverse.invoke(mDrawable);
            } catch (IllegalAccessException| NoSuchMethodException | InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        mDrawable.start();
        mAnimationReversed = !mAnimationReversed;
    }

Just verified: works on API21, but does not work on API24 :-(

Upvotes: 1

Related Questions