Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse animation in AnimatedVectorDrawable

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

like image 516
Anton Holovin Avatar asked Jun 13 '16 18:06

Anton Holovin


1 Answers

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 :-(

like image 98
Mikhail Avatar answered Nov 13 '22 05:11

Mikhail