如何让文字在Android中淡入淡出?

我有一段文字,当一个button被点击时,我希望这个文字淡出,改为其他文字,然后淡入。我有一些代码,但它不会淡出淡出animation。

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

你似乎在将它设置为熄灭之后,将animation设置为正确的。 这使得只有“在”animation工作。

要在第一个animation之后开始第二个animation,可以将侦听器添加到第一个animation中:

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

然后,而不是开始,开始。

我希望这有帮助!

如果要使用Animation ,则可以使用AnimatorListener来侦听第一个animation何时完成,然后开始第二个animation。 这将onAnimationEnd()

更多信息在这里: http : //developer.android.com/reference/android/animation/Animator.AnimatorListener.html

有可能是一个更好的方法来做到这一点与AnimationSet ,但这是肯定的。

你应该考虑使用像TextSwitcher东西。 Android文档中有一个关于TextSwitcher的简要文档。 我最好推荐的是查看API演示,使用TextSwitcher之一是一个很好而且简单的方法。 下载API演示,并自己检查出来,或在这里看到。

添加到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> 

你在你的activity / fragment / dialogfragment中使用这个variables,下面是我在我的使用的variables…

 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[]{""}; 

现在的对象和variablesinnitialized是这样使用的,我用这个我的对话框片段,在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); 

当我有大量的文本FadeIn / FadeOut,我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 } }); }