2010-08-19 2 views
16

J'ai deux TranslateAnimations sur un TextView et je veux qu'ils s'exécutent les uns après les autres. Cependant, en utilisant le code ci-dessous, seul le second est exécuté.Animation Android l'un après l'autre

Comment puis-je résoudre ce problème?

TranslateAnimation animation = new TranslateAnimation(
    Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, 0.0f, 
    Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, -150.0f); 
animation.setDuration(200); 
wave.startAnimation(animation); 

TranslateAnimation animation1 = new TranslateAnimation(
    Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, 0.0f, 
    Animation.ABSOLUTE, 150.0f, Animation.ABSOLUTE, 0.0f); 
animation1.setDuration(200); 
wave.startAnimation(animation1); 
+5

ce qui est ici vague? –

Répondre

29

EDIT: Bottes Andy réponse ci-dessous est la meilleure réponse imo.


Il suffit de définir votre premier comme ça et ça va commencer l'autre, une fois les finitions d'animation:

animation.setAnimationListener(new AnimationListener() { 

     @Override 
     public void onAnimationStart(Animation animation) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onAnimationRepeat(Animation animation) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onAnimationEnd(Animation animation) { 
      wave.startAnimation(animation1); 

     } 
    }); 

modifier: La seule raison de votre deuxième animation est exécutée avec votre code actuel, est parce que cela annule le jeu de la première animation (les deux sont réellement joués, mais vous ne voyez que le dernier à démarrer). Si vous aimez comme j'ai écrit, ils joueront séquentiellement au lieu de en parallèle.

+0

Merci Essayez-le et cela fonctionne comme je le voulais ... mais juste curieux, y at-il une autre meilleure façon de le faire? – amithgc

+0

pas que je sache, sry; _; – pgsandstrom

+0

Merci. Ca marche comment je cherche. . . –

49

Lien ensemble avec Animation Set

AnimationSet as = new AnimationSet(true) 
TranslateAnimation animation = new TranslateAnimation(
Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, 0.0f, 
Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, -150.0f); 
animation.setDuration(200); 
as.addAnimation(animation); 

TranslateAnimation animation1 = new TranslateAnimation(
Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, 0.0f, 
Animation.ABSOLUTE, 150.0f, Animation.ABSOLUTE, 0.0f); 
animation1.setDuration(200); 
animation1.setStartOffset(200); 
as.addAnimation(animation1); 

wave.startAnimation(as); 
+1

C'est merveilleux. Cependant, cela semble sérialiser les animations uniquement pour un objet. Si vous devez sérialiser des animations pour différents objets, vous devrez utiliser différentes approches. –

+0

cela a fonctionné pour moi, mais dans le mauvais ordre, l'animation que j'ai mis en premier est venu en second lieu, comment résoudre? – underfilho

6

Il y a une approche plus pour atteindre cet objectif qui peut être utile lorsque vous avez besoin pour animer un grand nombre de vues les unes après les autres. Vous pouvez utiliser la méthode setStartOffset pour définir un délai avant le début de l'animation. Donc, si vous savez, combien de temps prendra votre première animation pour terminer, vous pouvez définir cela comme un délai pour votre deuxième animation. Ceci est un exemple où j'ai animé six ImageButtons et six TextViews ci-dessous les uns après les autres:

public void animateButtons() { 
    // An array of buttons 
    int[] imageButtonIds = {R.id.searchButton, R.id.favoriteButton, R.id.responseButton, R.id.articleButton, R.id.resumeButton, R.id.subscribeButton}; 
    // Array of textViews 
    int[] textViewIds = {R.id.searchTextView, R.id.favoriteTextView, R.id.responseTextView, R.id.articleTextView, R.id.resumeTextView, R.id.subscribeTextView}; 

    int i = 1; 

    for (int viewId : imageButtonIds) { 

     ImageButton imageButton = (ImageButton) findViewById(viewId); 
     // Animation from a file fade.xml in folder res/anim 
     Animation fadeAnimation = AnimationUtils.loadAnimation(this, R.anim.fade); 
     // Delay for each animation is 100 ms bigger than for previous one 
     fadeAnimation.setStartOffset(i * 100); 
     imageButton.startAnimation(fadeAnimation); 

     // The same animation is for textViews 
     int textViewId = textViewIds[i-1]; 
     TextView textView = (TextView) findViewById(textViewId); 
     textView.startAnimation(fadeAnimation); 

     i ++; 
    } 
} 

Dans mon dossier res/anim J'ai un fichier, appelé fade.xml avec ces contenus:

<?xml version="1.0" encoding="utf-8"?> 

<!-- Fade animation with 500 ms duration --> 

<alpha xmlns:android="http://schemas.android.com/apk/res/android" 
     android:interpolator="@android:anim/accelerate_decelerate_interpolator" 
     android:fromAlpha="0.0" android:toAlpha="1.0" 
     android:duration="500" /> 
+0

Bon un merci –

+0

pas fonctionné pour moi | -) fadeAnimation.setStartOffset change l'animation racine. y-a-t'il une solution? – Solivan

6

vous pouvez également Pour ce faire, XML lui-même en utilisant l'attribut android:startOffset, et il y a une examble:

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <scale 
     android:duration="300" 
     android:fromXScale="0%" 
     android:fromYScale="0%" 
     android:pivotX="50%" 
     android:pivotY="50%" 
     android:toXScale="100%" 
     android:toYScale="100%" /> 
    <alpha 
     android:duration="300" 
     android:fromAlpha="0" 
     android:toAlpha=".5" /> 
    <alpha 
     android:duration="300" 
     android:fromAlpha=".5" 
     android:startOffset="300" 
     android:toAlpha="1" /> 

</set> 
-1

Si yo vous utilisez le code, vous pouvez appeler

Animation.setStartOffset() 

pour retarder la deuxième animation. Si vous utilisez xml, vous pouvez utiliser la propriété android:ordering="sequentially" pour que les deux animations se déroulent de manière séquentielle.

1

Créez un tableau d'animation et utilisez la méthode pour créer AnimationSet.

Animation[] animations = { 
      getScaleAnimation(0.4f, 1.3f, 2000), 
      getScaleAnimation(1.3f, 1.0f, 500), 
      getScaleAnimation(0.4f, 1.3f, 1000), 
      getScaleAnimation(1.3f, 1.0f, 3000), 
      getScaleAnimation(0.4f, 1.3f, 500), 
      getScaleAnimation(1.3f, 1.0f, 1700), 
      getScaleAnimation(0.4f, 1.3f, 2100), 
      getScaleAnimation(1.3f, 1.0f, 3400) 
    }; 
    AnimationSet animationSet = addAnimationAr(animations); 
    view.startAnimation(animationSet); 

Méthode:

public static AnimationSet addAnimationAr(Animation[] animations) { 
    AnimationSet animationSet = new AnimationSet(false); 
    long totalAnimationDuration = 0; 

    for (int i = 0; i < animations.length; i++) { 
     Animation a = animations[i]; 
     a.setStartOffset(totalAnimationDuration); 
     totalAnimationDuration += a.getDuration(); 
     animationSet.addAnimation(a); 
    } 

    return animationSet; 
}