Reverse AnimatorSet

为君一笑 提交于 2021-01-28 00:09:11

问题


Is there a way to run an AnimatorSet in reverse on Android? The ValueAnimator API does provide a reverse method on the individual animators but not on a set of animators.


回答1:


If your AnimatorSet is being played sequentially then you could use the method mentioned by @blackbelt:

public static AnimatorSet reverseSequentialAnimatorSet(AnimatorSet animatorSet) {
    ArrayList<Animator> animators = animatorSet.getChildAnimations();
    Collections.reverse(animators);

    AnimatorSet reversedAnimatorSet = new AnimatorSet();
    reversedAnimatorSet.playSequentially(animators);
    reversedAnimatorSet.setDuration(animatorSet.getDuration());

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        // getInterpolator() requires API 18
        reversedAnimatorSet.setInterpolator(animatorSet.getInterpolator());
    }
    return reversedAnimatorSet;
}

The caveat being that this only works for simple sequential animations as any dependencies setup in the original AnimatorSet will be lost. Also, if an interpolator was used on the AnimatorSet it will only carry over on API 18 or newer (per method mentioned above, you could alternatively manually add the interpolator back to the new reversed animator set).

The individual animations within the AnimatorSet will not play in reverse, if that is desirable then you'll also have to iterate over the animations of the AnimatorSet and set a ReverseInterpolator on each, see answer to Android: Reversing an Animation.




回答2:


You can take the initial input values from i.e ValueAnimator.ofFloat(0, 1) and switch them around with yourAnimator.setFloatValues(1, 0) before calling yourAnimatorSet.start() when you want to reverse animation




回答3:


The reverse() method has been added in API 26:

Plays the AnimatorSet in reverse. If the animation has been seeked to a specific play time using setCurrentPlayTime(long), it will play backwards from the point seeked when reverse was called. Otherwise, then it will start from the end and play backwards. This behavior is only set for the current animation; future playing of the animation will use the default behavior of playing forward.

Note: reverse is not supported for infinite AnimatorSet.



来源:https://stackoverflow.com/questions/33025396/reverse-animatorset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!