android – Start multiple ViewPropertyAnimators at the same time-ThrowExceptions

Exception or error:

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)

How to solve:

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.

###

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.

Leave a Reply

Your email address will not be published. Required fields are marked *