2010-08-19 12 views
16

Tengo dos TranslateAnimations en un TextView y quiero que se ejecuten uno tras otro. Sin embargo, al usar el código a continuación, solo se ejecuta el segundo.Android Animation uno después de otro

¿Cómo puedo solucionar esto?

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

lo que es onda aquí? –

Respuesta

29

EDIT: Andy Botas respuesta a continuación es la mejor respuesta de la OMI.


acaba de establecer su primero como esto y va a empezar el otro, una vez que los acabados de animación:

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); 

     } 
    }); 

edición: La razón única de su segunda animación se ejecuta con su código actual, es porque anula la reproducción de la primera animación (en realidad ambas se reproducen, pero solo se ve la última para comenzar). Si lo haces como escribí, jugarán secuencialmente en lugar de en paralelo.

+0

Gracias Lo probé y funciona como yo quería ... pero con curiosidad, ¿hay alguna otra forma mejor de hacerlo? – amithgc

+0

no que yo sepa, sry; _; – pgsandstrom

+0

Gracias. Funciona como estoy buscando. . . –

49

vincularlos con 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

Esto es maravilloso. Sin embargo, esto parece serializar animaciones solo para un objeto. Si tiene que serializar animaciones para diferentes objetos, deberá usar diferentes enfoques. –

+0

esto funcionó para mí, pero en el orden incorrecto, la animación que puse primero quedó en segundo lugar, ¿cómo resolverla? – underfilho

6

Hay otro método para alcanzar este objetivo que puede ser útil cuando se necesita para animar una gran cantidad de puntos de vista, uno tras otro. Puede usar el método setStartOffset para establecer un retraso antes de que comience la animación. Por lo tanto, si sabe cuánto tiempo le tomará a su primera animación finalizar, puede establecer esto como un retraso para su segunda animación. Este es un ejemplo en el animé seis ImageButtons y seis TextViews por debajo de ellos, uno tras otro:

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 ++; 
    } 
} 

En mi res/anim carpeta Tengo un archivo, llamado fade.xml con estos contenidos:

<?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

Bueno, gracias –

+0

no funcionó para mí | -) fadeAnimation.setStartOffset cambia la animación de la raíz. ¿Hay alguna solución? – Solivan

6

también le puede hacer esto por el propio XML utilizando android:startOffset atributo, y hay una 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 Utilice el código, puede llamar al

Animation.setStartOffset() 

para retrasar la segunda animación.

si usa xml puede android:ordering="sequentially" propiedad para hacer que las dos animaciones se realicen secuencialmente.

1

Crea una matriz de animación y usa el método para crear 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étodo:

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; 
}