Start multiple ViewPropertyAnimators at the same time

11,362

Solution 1

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.

Solution 2

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.

Share:
11,362

Related videos on Youtube

Jakob
Author by

Jakob

Updated on September 15, 2022

Comments

  • Jakob
    Jakob over 1 year

    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)

    • Alireza Ahmadi
      Alireza Ahmadi over 9 years
      So why you don't use Animator class?!
    • Russ Wheeler
      Russ Wheeler over 9 years
      @Jakob did you find any way of doing this? I'm thinking of doing the same and the only way I can think of so far is to add a withStartAction and inside the Runnable, to kick off another animation.
    • Richard Onslow Roper
      Richard Onslow Roper almost 3 years
      Or you could use ViewPropertyAnimatorCompatSet. You know, project size and all.
  • Jakob
    Jakob over 9 years
    Thanks Russ. This is a good solution if you want to chain a set of animations together, and play on after the other. I, however, as stated in the question, am looking to animate multiple listView items, and these animations are to run simultaneously. I'll give you a +1 though, or what it is called.
  • Russ Wheeler
    Russ Wheeler over 9 years
    Ok, I was sure I was using that command in my code, however, what I'm doing instead is putting my animations in a runnable, and then adding those runnables into an array of runnables. I am then looping through the array and starting them all with run(). I'm not sure if this will work in your situation as I will only have two or three animations running at one time, so in my situation the loop is processed more or less instantly. However, if you have a few hundred animations to run at once then they won't appear to do that (though you have a listview so you can't have THAT many animations)
  • Carlo Conserva
    Carlo Conserva over 6 years
    I think it's important to point out that Bartek's wrapper only gives you the same syntax of ViewPropertyAnimator, but not the same optimisations. As the documentation says, the main advantage of ViewPropertyAnimator over ObjectAnimator is that the former is more optimised when it comes to invalidate and redraw only the needed portions of the view.
  • Bartek Lipinski
    Bartek Lipinski over 6 years
    Yes, that's true. The main goal for my wrapper is to reduce the overhead of writing the same type of "wrapper code" when you actually need an ObjectAnimator. I don't look at it as a substitute for ViewPropertyAnimator. Thanks for clarifying @CarloConserva!