How to make text fade in and out in Android?

后端 未结 6 560
说谎
说谎 2021-01-30 12:44

I have a paragraph of text and when a button is clicked I want that text to fade out, change to some other text, then fade back in. I have some code but it doesn\'t do the fade

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-30 13:20

    When I have an amount of texts to FadeIn / FadeOut , I prefere use a function to do this:

    protected void onCreate(Bundle savedInstanceState) {
    {
    ...
    //Declare the array of texts:
    String aSentences[]={"Sentence 1", "Sentence 2", "Sentence 3"};
    TextView tView = (TextView)findViewById(R.id.Name_TextView_Object);
    
    //call the function:
    animateText(tView,aSentences,0,false);
    }
    
    private void animateText(final TextView textView, final String texts[], final int textIndex, final boolean forever) {
            //textView <-- The View which displays the texts
            //texts[] <-- Holds R references to the texts to display
            //textIndex <-- index of the first text to show in texts[]
            //forever <-- If equals true then after the last text it starts all over again with the first text resulting in an infinite loop. You have been warned.
    
            int fadeInDuration = 1000; // Configure time values here
            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()); // add this
            fadeIn.setDuration(fadeInDuration);
    
            Animation fadeOut = new AlphaAnimation(1, 0);
            fadeOut.setInterpolator(new AccelerateInterpolator()); // and this
            fadeOut.setStartOffset(fadeInDuration + timeBetween);
            fadeOut.setDuration(fadeOutDuration);
    
            AnimationSet animation = new AnimationSet(false); // change to 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); //Calls itself until it gets to the end of the array
                    }
                    else {
                        textView.setVisibility(View.VISIBLE);
                        if (forever == true){
                            animateText(textView, texts, 0,forever);  //Calls itself to start the animation all over again in a loop if forever = true
                        }
                        else
                            {//do something when the end is reached}
                    }
                }
                public void onAnimationRepeat(Animation animation) {
                    // TODO Auto-generated method stub
                }
                public void onAnimationStart(Animation animation) {
                    // TODO Auto-generated method stub
                }
            });
        }
    

提交回复
热议问题