How to use androidx.animation.AnimatorSet?

293 Views Asked by At

I need to use reverse() method of Android AnimatorSet class. The problem is that this method was added only in api 26. But I found reverse method in source code in package androidx.animation. And I couldn't find any androidx library that allows to use that AnimatorSet (androidx.animation.AnimatorSet). How can I use that class?

2

There are 2 best solutions below

0
On

If you don't care it's still in alpha, you can import it from here:

// To use the Animator APIs
implementation "androidx.core:core-animation:1.0.0-alpha01"
// To test the Animator APIs
androidTestImplementation "androidx.core:core-animation-testing:1.0.0-alpha01"
```
2
On

For Api >=26 you just create an AnimatorSet object and after you define the animation you call reverse on it:

AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playSequentially(ObjectAnimator.ofFloat(...), ...); 
animatorSet.setDuration(...);
animatorSet.reverse();
animatorSet.start();

For older APIs you just reverse the order within the .ofFloat methods in your ObjectAnimators:

If initially you have ObjectAnimator.ofFloat(0, 1) you change it to animator.setFloatValues(1, 0)