在AnimatedVectorDrawable中实现反向动画

8

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


1
我相信你需要自己制作一个反向动画。我认为安卓系统没有提供这样的功能。 - Stardust
1
@Stardust 你是对的。我实现了两个独立的动画。 - Anton Holovin
1个回答

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

你可以使用反射来调用这个方法。例如像这样:
    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;
    }

刚刚验证:在API21上可以工作,但在API24上无法工作 :-(


4
reverse(); 方法在 API24 中已经不再存在。 - Mark

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接