48 votes

Comment faire disparaître et réapparaître du texte sur Android ?

J'ai un paragraphe de texte et quand un bouton est cliqué, je veux que le texte s'estompe, change pour un autre texte, puis réapparaisse. J'ai du code mais il ne fait pas l'animation de disparition, juste l'apparition.

    final TextView mSwitcher = (TextView) findViewById(R.id.bookContent);
    mSwitcher.setText("ancien texte");

    final Animation in = new AlphaAnimation(0.0f, 1.0f);
    in.setDuration(3000);

    final Animation out = new AlphaAnimation(1.0f, 0.0f);
    out.setDuration(3000);

    Button moveOn = (Button) findViewById(R.id.moveOn);
    moveOn.setOnClickListener( new OnClickListener() {
        public void onClick(View v) {

            mSwitcher.startAnimation(out);
            mSwitcher.setText("nouveau texte");
            mSwitcher.startAnimation(in);

        }
    });

0voto

Edimar Martins Points 19

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

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X