2016-05-27 31 views
0

我做的,我想用動畫的Android應用:在一個單一的TextView使用淡出動畫三次,而每個動畫後更改文本

  1. 「text1」中出現在屏幕中央的文本。
  2. 然後這個文本在1秒內消失,然後另一個文本「text2」出現在同一個地方。
  3. 然後「text2」在1秒內消失,最後另一個文本「text3」出現在同一個地方並保持在屏幕上。

我所做的是創建了一個TextView。

TextView textview; 
textview = (TextView) findViewById(R.id.text); 
Animation r1; 
r1 = AnimationUtils.loadAnimation(this, R.anim.fadeout); 
textview.setText("text1"); 
textview.startAnimation(r1); 
textview.setText("text2"); 
textview.startAnimation(r1); 
textview.setText("text3"); 

但它不起作用。

這是淡出動畫我用:

<?xml version="1.0" encoding="utf-8"?> 
<alpha xmlns:android="http://schemas.android.com/apk/res/android" 
android:interpolator="@android:anim/decelerate_interpolator" 
android:fromAlpha="1.0" 
android:toAlpha="0.0" 
android:duration="1000" 
/> 
+0

http://stackoverflow.com/questions/29218173/android-text-fade-in-and-out –

回答

0

使用AnimationListener用於這一目的

Animation r1,r2,r3; 
r1 = AnimationUtils.loadAnimation(this, R.anim.fadeout); 
r2 = AnimationUtils.loadAnimation(this, R.anim.fadeout); 
r3 = AnimationUtils.loadAnimation(this, R.anim.fadeout); 
textview.setText("text1"); 
textview.startAnimation(r1); 

r1.setAnimationListener(new AnimationListener(){   
      @Override 
      public void onAnimationEnd(Animation fade1) 
      { 

      textview.setText("text2"); 
      textview.startAnimation(r2); 
      } 

      @Override 
      public void onAnimationRepeat(Animation animation) {} 

      @Override 
      public void onAnimationStart(Animation animation) {} 
     }); 


r2.setAnimationListener(new AnimationListener(){   
      @Override 
      public void onAnimationEnd(Animation fade1) 
      { 
       textview.setText("text3"); 
      textview.startAnimation(r3); 
      } 

      @Override 
      public void onAnimationRepeat(Animation animation) {} 

      @Override 
      public void onAnimationStart(Animation animation) {} 
     }); 
+0

Yeahh這工作,但我們可以在三個動畫之間添加一個200毫秒的暫停? –

+0

可以在使用handler.postdelay延遲一段時間後調用第二和第三個動畫。去谷歌上查詢。 – KDeogharkar

0

嘗試這樣的事情

public class FadeOutInActivity extends Activity 
{ 
Handler mHandler; 
TextView mTextView; 

Animation in; 
Animation out; 
int mTotalCount; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_show); 

    mTotalCount = 0; 

    mHandler = new Handler(); 
    mTextView = (TextView) findViewById(R.id.textView); 

    in = new AlphaAnimation(0.0f, 1.0f); 
    in.setDuration(1000); 

    out = new AlphaAnimation(1.0f, 0.0f); 
    out.setDuration(1000); 
    out.setAnimationListener(new Animation.AnimationListener() { 

     @Override 
     public void onAnimationEnd(Animation animation) { 
      mTotalCount++; 

      if (mTotalCount == 1) { 
       mTextView.setText("text2"); 
      } else if (mTotalCount == 2) { 
       mTextView.setText("text3"); 
      } 

      mTextView.startAnimation(in); 
      mHandler.postDelayed(mFadeOut, 1000); 

     } 

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

     } 

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

     } 
    }); 
    //mSwitcher.startAnimation(out); 
    mTextView.setText("text1"); 
    mTextView.startAnimation(in); 
    mHandler.postDelayed(mFadeOut, 1000); 

} 

private Runnable mFadeOut =new Runnable(){ 

    @Override 
    public void run() { 
     //Speed up the last fade-out so that the Activity opens faster 

     out.setDuration(1000); 

     mTextView.startAnimation(out); 
    } 
}; 
} 

和佈局這樣

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent"> 
<TextView 
    android:id="@+id/textView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="center_vertical" 
    android:layout_centerInParent="true" 
    android:text="Name" 
    android:textSize="20dp" 
    android:textStyle="bold" /> 

</RelativeLayout> 
相關問題