web-dev-qa-db-fra.com

Android - Image clignotante utilisant l'animation Alpha fade

Je me bats depuis quelques jours à ce sujet, j'ai finalement décidé de poser la question. C'est tellement simple qu'il me manque quelque chose de très fondamental. 

J'ai une page de mise en page XML avec une image définie. J'ai deux pages XML anim, une pour changer alpha de 0 à 1 et l'autre de 1 à 0 afin de créer un effet "clignotant". Donc, l'alphaAnimation est défini en XML, je dois juste l'appeler. 

L'image apparaît, mais il n'y a pas d'effet de clignotement en boucle. 

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

Deux dispositions d'animation XML dans la ressource Anim:

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

Et l'autre:

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

Pourquoi ne pas utiliser Android:repeatMode="reverse" 

29
methodin

Le meilleur moyen de fondre en interpolation:

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

dans votre res/anim/créer tween.xml tween 1s commencer l'opacité 0 à 1 et inverser l'infini ...

<?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>
92
Buchs sullivan

Vous pouvez utiliser l'animation alpha ci-dessous pour définir les effets clignotants sur les vues dans 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);

Après cela, ajoutez l'animation à votre vue comme,

view.setAnimation(blinkanimation); 

ou

view.startAnimation(blinkanimation);
15
KarthikKPN

Si quelqu'un décide d'utiliser la version programmatique:

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)
1
Zakir

Si vous souhaitez créer une animation de fondu sur une ImageView, mais que vous ne voulez pas le faire clignoter vous-même ou que vous ne voulez pas que l'arrière-plan clignote, une vue personnalisée est la seule approche. Voir ma réponse ici: https: // stackoverflow.com/a/50299715/2102748

0
milosmns