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