Android alfa animación fadein fadeout con retrasos


Quiero hacer una animación alfa muy simple pero no puedo encontrar una manera válida.

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

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

He intentado implementar eso 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 parece que la tercera animación hace un lío con todas las animaciones alfa, supongo que esto causa una incoherencia interna en la forma en que Android gestiona este tipo de animación.

Alguna idea?

Gracias.

Author: Kling Klang, 2010-07-21

2 answers

Ok Tenga en cuenta estos 2 puntos para resolver esto


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

    Para lograr esto:

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

  • Solo necesitas 2 animaciones que se repetirán para siempre.

    1.0.0f to 1.0f con pausa de 5 segundos y duración de 1 segundo

    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 hacer un bucle para siempre usaré 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);
 105
Author: Sherif elKhatib,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-08-15 16:18:05

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

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

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

De la misma manera puede agregar oyentes de animación.

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

 16
Author: amalBit,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-08-30 17:40:27