2011-12-24 105 views
43

我有一段文字,當點擊一個按鈕時,我希望文字淡出,改爲其他文字,然後淡入。我有一些代碼,但它不會淡出淡出動畫只是淡入淡出in。如何讓文字在Android中淡入淡出?

final TextView mSwitcher = (TextView) findViewById(R.id.bookContent); 
    mSwitcher.setText("old text"); 

    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("new text"); 
      mSwitcher.startAnimation(in); 

     } 
    }); 

回答

73

您似乎將動畫設置爲在將其設置爲熄滅之後。這隻會讓「in」動畫工作。

爲了使第二動畫之後的第一次啓動,你可以將監聽器添加到您的第一個動畫:

out.setAnimationListener(new AnimationListener() { 

    @Override 
    public void onAnimationEnd(Animation animation) { 
     mSwitcher.setText("New Text"); 
     mSwitcher.startAnimation(in); 

    } 
}); 

然後,在你onClick()方法:

public void onClick(View v) { 

    mSwitcher.startAnimation(out); 

} 

這應該做的招。


另一種方法是使用AnimationSet

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

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

AnimationSet as = new AnimationSet(true); 
as.addAnimation(out); 
in.setStartOffset(3000); 
as.addAnimation(in); 

然後,而不是啓動out,開始as

我希望這有助於!

+2

出色答卷! :) – 2011-12-24 23:04:33

+0

@eboxis感謝您的知識分享..有沒有什麼辦法按順序執行多個視圖的動畫..我試着用第一個視圖的animationEnd中的第二個視圖開始動畫...但是當我這樣做時.. .first視圖再次獲得動畫...任何建議...? – CoDe 2014-02-17 13:41:48

+0

令人驚歎!第二種方法爲我做了詭計!謝謝:) – 2016-03-02 07:05:09

2

你應該考慮使用類似TextSwitcher的東西。 Android文檔中的TextSwitcher上有一個簡短的document。我最好推薦的是看看API演示,使用TextSwitcher之一是一個偉大而簡單的方法。下載API演示並自行檢查,或參見here

2

要添加到eboix答案...這就是我淡入文本和淡出文本,每次淡入和淡出之間的延遲(即淡入之後)。

我的XML看起來像這樣。

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:gravity="center" 
    android:text="Retrieving Result" 
    android:textColor="@color/general_app_colour" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <TextView 
    android:id="@+id/blobText" 
    android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:gravity="center" 
     android:text="Please Wait" /> 

</LinearLayout> 

的你在活動/片段/ dialogfragment使用這個變量,下面是我在我使用的變量...

public class Loading_Dialog extends DialogFragment { 
    public String[] text = new String[]{""}; 
    TextView blobText; 
    Animation inAnimation; 
    Animation displayLength; 
    Animation delayAnimation; 
    Animation outAnimation; 
    //duration for fade effects 
    int fadeEffectDuration = 700; 
    //duration for delay between fadeout and fadein 
    int delayDuration = 1000; 
    int displayFor = 2000; 
    public String[] text = new String[]{""}; 

現在的對象和變量innitialized是像這樣使用,我用這個我的對話框片段,在oncreateDialog方法..

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 

    final Dialog dialog = new Dialog(getActivity(),R.style.LoadingDialogAnimation); 
dialog.getWindow().setContentView(R.layout.dialog_loading); 
blobText = (TextView) dialog.findViewById(R.id.blobText); 
    inAnimation = new AlphaAnimation(0f, 1f); 
    inAnimation.setDuration(fadeEffectDuration);   
    displayLength = new AlphaAnimation(1f, 1f); 
    displayLength.setDuration(displayFor); 
    delayAnimation = new AlphaAnimation(0f, 0f); 
    delayAnimation.setDuration(delayDuration); 
    outAnimation = new AlphaAnimation(1f, 0f); 
    outAnimation.setDuration(fadeEffectDuration); 
    inAnimation.setAnimationListener(new AnimationListener() { 
    @Override 
    public void onAnimationStart(Animation animation) { 
     position++; 
    if(position>=text.length) 
    { 
     position = 0; 
    } 
    blobText.setText(text[position]); 
}   
@Override 
public void onAnimationRepeat(Animation animation) {}   
@Override 
public void onAnimationEnd(Animation animation) { 
    blobText.startAnimation(displayLength); 
} 
}); 

displayLength.setAnimationListener(new AnimationListener() { 

@Override 
public void onAnimationStart(Animation animation) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void onAnimationRepeat(Animation animation) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void onAnimationEnd(Animation animation) { 
    // TODO Auto-generated method stub 
    blobText.startAnimation(outAnimation); 
} 
}); 

    outAnimation.setAnimationListener(new AnimationListener() { 
    @Override 
    public void onAnimationStart(Animation animation) { 
     // TODO Auto-generated method stub 
    } 

    @Override 
    public void onAnimationRepeat(Animation animation) { 
     // TODO Auto-generated method stub 
    } 

    @Override 
    public void onAnimationEnd(Animation animation) { 
     // TODO Auto-generated method stub 
     blobText.startAnimation(delayAnimation);  
    } 
    }); 
    delayAnimation.setAnimationListener(new AnimationListener() { 

    @Override 
    public void onAnimationStart(Animation animation) { 
    // TODO Auto-generated method stub 
    } 

    @Override 
    public void onAnimationRepeat(Animation animation) { 
    // TODO Auto-generated method stub 

    } 

@Override 
public void onAnimationEnd(Animation animation) { 
// TODO Auto-generated method stub 
    blobText.startAnimation(inAnimation); 
} 
}); 

blobText.startAnimation(outAnimation); 
0

當我有文本,以淡入/淡出的量,我prefere用一個函數來做到這一點:

protected void onCreate(Bundle savedInstanceState) { 
{ 
... 
//Declare the array of texts: 
String aSentences[]={"Sentence 1", "Sentence 2", "<b><i>Sentence 3</i></b>"}; 
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 
      } 
     }); 
    }