2010-07-21 23 views
34

Quiero hacer una animación alfa muy simple pero no puedo encontrar una forma válida.Android alpha animation fadein fadeout with retrasys

La idea es realizar esta animación sobre una vista:

  1. alpha de 0 a 1 de 1 segundo
  2. asimiento alfa a 1 durante 5 segundos
  3. alfa de 1 a 0 de 1 segundo
  4. mantenga alfa en 0 durante 5 segundos.
  5. empiezan de nuevo en 1.

He tratado de poner en práctica que con un AnimationSet como:

AnimationSet animationSet = new AnimationSet(true); 

Animation animation1 = new AnimationUtils.loadAnimation(this, android.R.anim.fade_in); 
animation1.setDuration(1000); 

Animation animation2 = new AnimationUtils.loadAnimation(this, android.R.anim.fade_out); 
animation2.setDuration(1000); 
animation2.setStartOffset(5000); 

Animation animation3 = new AlphaAnimation(0.0f, 0.0f); 
animation3.setDuration(4000) 
animation3.setStartOffset(6000); 

animationSet.add(animation1); 
animationSet.add(animation2); 
animationSet.add(animation3); 

etc ..

pero las costuras que la tercera animación hacer un lío con todas las animaciones alfa, supongo que esto causa una incoherencia interna en la forma en que Android administra este tipo de animación.

¿Alguna idea?

Gracias.

+0

¿Puede decirme si mi código funcionó bien? Y acepte la respuesta si es –

Respuesta

101

Mantener Ok en cuenta estos 2 puntos para resolver este


  • Si quiero animar 1.0f to 0.0f después de 5 segundos con una duración de la animación de 1 segundo, esto es en última instancia una segunda animación 1 con una pausa de 5 segundos.

    Para lograr esto:

    1. setDuration(1000) (que tiene de 1 segundo de duración)
    2. setStartOffset(5000) (que comenzará después de 5 segundos)

  • Solo necesitas 2 animaciones que l oop para siempre

    1. 0.0f to 1.0f con un 5 segundos de pausa y 1 segundo de duración

    2. 1.0f to 0.0f con 5 segundos de pausa y 1 segundo de duración


Y aquí está el código:

animation1 = new AlphaAnimation(0.0f, 1.0f); 
    animation1.setDuration(1000); 
    animation1.setStartOffset(5000); 

    animation2 = new AlphaAnimation(1.0f, 0.0f); 
    animation2.setDuration(1000); 
    animation2.setStartOffset(5000); 

    textView.startAnimation(animation1); 

Sin embargo, para el ciclo siempre voy a utilizar AnimationListener porque repeatCount tiene errores:

animation1 = new AlphaAnimation(0.0f, 1.0f); 
    animation1.setDuration(1000); 
    animation1.setStartOffset(5000); 

    //animation1 AnimationListener 
    animation1.setAnimationListener(new AnimationListener(){ 

     @Override 
     public void onAnimationEnd(Animation arg0) { 
      // start animation2 when animation1 ends (continue) 
      textView.startAnimation(animation2); 
     } 

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

     } 

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

     } 

    }); 

    animation2 = new AlphaAnimation(1.0f, 0.0f); 
    animation2.setDuration(1000); 
    animation2.setStartOffset(5000); 

    //animation2 AnimationListener 
    animation2.setAnimationListener(new AnimationListener(){ 

     @Override 
     public void onAnimationEnd(Animation arg0) { 
      // start animation1 when animation2 ends (repeat) 
      textView.startAnimation(animation1); 
     } 

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

     } 

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

     } 

    }); 

    textView.startAnimation(animation1); 
+0

¡Respuesta perfecta! ¡Muchas gracias! – David

+0

bien, ¿puede esperar esperar 5 segundos y luego mostrar algo? con el fin de manipular.post retraso. – mehmet

14

Hay una solución más simple para esto.

Supongamos que su vista está en el estado GONE. Para animar a su visibilidad:

yourView.setVisibility(View.VISIBLE); 
yourView.animate().alpha(1).setDuration(300); 

Del mismo modo puede agregar oyentes de animación.

Esto también funciona para animaciones de escala y traducción.

+1

Muy bien. Gracias. – paakjis