Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start multiple ViewPropertyAnimators at the same time

With the Animator class you can simply call something like the following to play multiple animations simultaneously:

AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(animatorsArray);
animatorSet.start();

But I cannot find anything similar which would work with ViewPropertyAnimator.

(FYI. I am trying to animate multiple listView items)

like image 840
Jakob Avatar asked Aug 09 '14 09:08

Jakob


2 Answers

I know the question is more than one year old, but since I needed the same thing and I came up with a solution I decided to share it:

I've created an ObjectAnimator wrapper which you can use in almost exactly the same manner as you would use ViewPropertyAnimator. And still, you can use the ObjectAnimator object so you can write your AnimatorSet.

The wrapper is available here.

Example (setting up an animation of the same parameters for a mTestView):

  1. ViewPropertyAnimator

mTestView.animate().withLayer().alphaBy(0.3f).rotationX(27);
  1. ViewPropertyObjectAnimator (my wrapper)

ObjectAnimator objectAnimator = 
    ViewPropertyObjectAnimator.animate(mTestView).withLayer().alphaBy(0.3f).rotationX(27).get();

and you have an ObjectAnimator which you can either just start() or use inside AnimatorSet.

like image 108
Bartek Lipinski Avatar answered Nov 01 '22 07:11

Bartek Lipinski


I would suggest using withStartAction as I mentioned above.

On further reading of ViewPropertyAnimator page in Android docs

public ViewPropertyAnimator withStartAction (Runnable runnable) Added in API level 16

Specifies an action to take place when the next animation runs. If there is a startDelay set on this ViewPropertyAnimator, then the action will run after that startDelay expires, when the actual animation begins. This method, along with withEndAction(Runnable), is intended to help facilitate choreographing ViewPropertyAnimator animations with other animations or actions in the application.

I am about to use this myself and it looks like it's working.

I added all my animation code into a Runnable, added multiple Runnables into an ArrayList and when ready, I looped through the ArrayList and called run() on all of them.

like image 6
Russ Wheeler Avatar answered Nov 01 '22 06:11

Russ Wheeler