Nicholette Liguori
Nicholette Liguori

Reputation: 65

Animate multiple views using the same Animation

I have a simple animator file:

<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:ordering="sequentially" >
<objectAnimator
    android:duration="1000"
    android:propertyName="x"
    android:repeatCount="1"
    android:repeatMode="reverse"
    android:valueTo="-50"
    android:valueType="floatType" />

Basically this takes a component and slides it along the X-axis by 50dp to the left. I've successfully attached this to ONE component, and it works flawlessly, but when I try to attach it to multiple components at once, the animation only works for the final component.

For Example: I have 5 cards. The AI enemy picks a random card from it's hand. But I would like to animate the enemy "picking" the card. This is where the animation comes into play.

So something like this:

AnimatorSet cardSet = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.pick_card);

for(int i=0; i<enemyCards.size(); i++){
   cardSet.setTarget(enemyCards.get(i));
   cardSet.start();    
}

The goal here, would be to loop through each card in the array and slide it out on the X axis. But the animation only occurs on the final card (the 5th card in the array)

Additionally (THIS IS BOLD FOR A REASON, people do not read large paragraphs anymore, and someone keeps editing my formatting) - I would like to have a delay each time the card slides out. So the loop should be something like:

loop{
    animate card 1
    delay
}

OR

the animator file should be something like

android:delay="100"

Ive been trying with little success

Upvotes: 1

Views: 1282

Answers (2)

azizbekian
azizbekian

Reputation: 62189

This a sketch code written in text editor, it may have syntax errors, but you'll get the concept:

AnimatorSet cardSet = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.anim.pick_card);

List<AnimatorSet> animators = new ArrayList<>(enemyCards.size());

for (int i = 0, size = animators.size(); i < size; ++i) {
    animators.add(cardSet.clone());
}

final int step = 100;
int delay = 0;

for (int i = 0, size = enemyCards.size(); i < size; ++i) {
    AnimatorSet set = animators.get(i);
    set.setTarget(enemyCards.get(i));
    set.setStartDelay(delay);
    delay += step;
    set.start();    
}

Upvotes: 1

user3307005
user3307005

Reputation: 216

You can collect your views in some viewgroup and than animate just parent view so all child views animated ?

Upvotes: 1

Related Questions