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.
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.