AnimatedVectorDrawable中的反向动画

Ant*_*vin 5 animation android android-vectordrawable

是否可以以相反的顺序播放AnimatedVectorDrawable的动画?

Mik*_*ail 1

如果你查看 AnimatedVectorDrawable 源代码,你会发现该方法

/**
 * 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()
Run Code Online (Sandbox Code Playgroud)

您可以使用反射来调用此方法。例如这样:

    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;
    }
Run Code Online (Sandbox Code Playgroud)

刚刚验证:适用于 API21,但不适用于 API24 :-(

  • API24 中不再存在 `reverse();` 方法 (4认同)