Android-Parpadeo de la imagen usando la animación Alpha fade


He estado luchando durante unos días en esto, finalmente decidí preguntar. Es tan simple que me falta algo muy básico.

Tengo una página de diseño XML con una imagen definida. Tengo dos páginas XML anim, una para cambiar alfa de 0 a 1, y la otra de 1 a 0 para crear un efecto de "parpadeo". Así que la alphaAnimation está definida en XML, solo necesito llamarlo.

La imagen aparece, pero no hay efecto de parpadeo en bucle.

public class blinker extends Activity {

   //create name of animation
Animation myFadeInAnimation;
Animation myFadeOutAnimation;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.scanning_view);

 //grab the imageview and load the animations
    ImageView myImageView = (ImageView) findViewById(R.id.blinkingView01); 
    Animation myFadeInAnimation = AnimationUtils.loadAnimation(null, R.anim.fade_in);
    Animation myFadeOutAnimation = AnimationUtils.loadAnimation(null, R.anim.fade_out);

//fade it in, and fade it out. 
    myImageView.startAnimation(myFadeInAnimation);
    myImageView.startAnimation(myFadeOutAnimation);
     }
}   

Dos XML Diseños de animación en Anim resource:

<?xml version="1.0" encoding="UTF-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <alpha android:fromAlpha="0.0" 
    android:toAlpha="1.0" 
    android:interpolator="@android:anim/accelerate_interpolator"  
    android:duration="50" android:repeatCount="infinite"/> 
 </set> 

Y el otro:

 <?xml version="1.0" encoding="UTF-8"?>
 <set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <alpha android:fromAlpha="1.0" android:toAlpha="0.0" 
    android:interpolator="@android:anim/accelerate_interpolator"  
    android:duration="1000" android:repeatCount="infinite"/> 
</set>
Author: Keith, 2010-10-11

5 answers

¿por Qué no usar android:repeatMode="reverse"

 28
Author: methodin,
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
2010-10-11 14:54:51

La mejor manera de interpolar se desvanecen :

ImageView myImageView = (ImageView) findViewById(R.id.imageView2); 
Animation myFadeInAnimation = AnimationUtils.loadAnimation(Splash.this, R.anim.tween);
myImageView.startAnimation(myFadeInAnimation);

En tu interpolación res/anim/ create.xml tween 1s inicia la opacidad de 0 a 1 e invierte el infinito...

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
    android:fromAlpha="0.0"
    android:toAlpha="1.0"
    android:duration="1000" 
    android:repeatMode="reverse"
    android:repeatCount="infinite" />
</set>
 89
Author: Buchs sullivan,
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
2015-06-19 10:46:10

Puede usar la siguiente animación alfa para establecer los efectos parpadeantes en las vistas en Android.

blinkanimation= new AlphaAnimation(1, 0); // Change alpha from fully visible to invisible
blinkanimation.setDuration(300); // duration
blinkanimation.setInterpolator(new LinearInterpolator()); // do not alter animation rate
blinkanimation.setRepeatCount(3); // Repeat animation infinitely
blinkanimation.setRepeatMode(Animation.REVERSE);

Después de esto agregue la animación a su vista como,

view.setAnimation(blinkanimation); 

O

view.startAnimation(blinkanimation);
 14
Author: KarthikKPN,
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-07-21 12:32:32

Si desea hacer la animación de fundido en una ImageView, pero no quiere parpadear usted mismo o no quiere que el fondo parpadee, una vista personalizada es el único enfoque-vea mi respuesta aquí: https://stackoverflow.com/a/50299715/2102748

 0
Author: milosmns,
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
2018-05-11 20:22:20

Si alguien decide usar la versión programática:

val anim = AlphaAnimation(1.0f, 0.0f)
anim.duration = 750
anim.fillAfter = true  

// here is repeat settings
anim.repeatMode = AlphaAnimation.REVERSE // ping pong mode
anim.repeatCount = 1 // count of repeats

yourView.startAnimation(anim)
 0
Author: Zakir,
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
2018-07-08 16:13:24