Lorsque j'ai une quantité de textes à FadeIn / FadeOut, je préfère utiliser une fonction pour le faire :
protected void onCreate(Bundle savedInstanceState) {
{
...
//Déclarer le tableau de textes :
String aSentences[]={"Phrase 1", "Phrase 2", "Phrase 3"};
TextView tView = (TextView)findViewById(R.id.Name_TextView_Object);
//appeler la fonction :
animateText(tView,aSentences,0,false);
}
private void animateText(final TextView textView, final String texts[], final int textIndex, final boolean forever) {
//textView <-- La vue qui affiche les textes
//texts[] <-- Contient les références R aux textes à afficher
//textIndex <-- index du premier texte à afficher dans texts[]
//forever <-- Si égal à true, il recommence à partir du premier texte après le dernier, ce qui crée une boucle infinie. Vous êtes averti.
int fadeInDuration = 1000; // Configurer les valeurs temporelles ici
int timeBetween = 5000;
int fadeOutDuration = 2000;
textView.setVisibility(View.INVISIBLE); //Visible or invisible by default - this will apply when the animation ends
textView.setText(Html.fromHtml(texts[textIndex]));
Animation fadeIn = new AlphaAnimation(0, 1);
fadeIn.setInterpolator(new DecelerateInterpolator()); // ajouter ceci
fadeIn.setDuration(fadeInDuration);
Animation fadeOut = new AlphaAnimation(1, 0);
fadeOut.setInterpolator(new AccelerateInterpolator()); // et cela
fadeOut.setStartOffset(fadeInDuration + timeBetween);
fadeOut.setDuration(fadeOutDuration);
AnimationSet animation = new AnimationSet(false); // changer en false
animation.addAnimation(fadeIn);
if((texts.length-1) != textIndex) animation.addAnimation(fadeOut);
animation.setRepeatCount(1);
textView.setAnimation(animation);
animation.setAnimationListener(new Animation.AnimationListener() {
public void onAnimationEnd(Animation animation) {
if (texts.length -1 > textIndex) {
animateText(textView, texts, textIndex + 1,forever); //S'appelle lui-même jusqu'à ce qu'il atteigne la fin du tableau
}
else {
textView.setVisibility(View.VISIBLE);
if (forever == true){
animateText(textView, texts, 0,forever); //Appelle lui-même pour démarrer l'animation depuis le début en boucle si forever = true
}
else
{//faire quelque chose lorsque la fin est atteinte}
}
}
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
});
}