TextSwitcher,一个带有文字切换动画效果的加强版TextView

先上图

TextSwitcher,一个带有文字切换动画效果的加强版TextView


直接上代码

[html]
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:orientation="vertical" android:layout_width="match_parent"  
  3.     android:gravity="center"  
  4.     android:layout_height="match_parent">  
  5.   
  6.     <!--inAnimation 进入时的动画-->  
  7.     <!--outAnimation 退出时的动画-->  
  8.     <TextSwitcher  
  9.         android:id="@+id/ts_test"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:inAnimation="@anim/anim_in"  
  13.         android:outAnimation="@anim/anim_out"  
  14.         />  
  15.   
  16.     <Button  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content"  
  19.         android:text="切换下一个"  
  20.         android:onClick="next"/>  
  21.   
  22. </LinearLayout>  

具体动画代码 退出

[html]
  1. <set xmlns:android="http://schemas.android.com/apk/res/android">  
  2.     <translate  
  3.         android:fromXDelta="0"  
  4.         android:fromYDelta="0"  
  5.         android:toXDelta="0"  
  6.         android:toYDelta="-100%"  
  7.         android:duration="500"/>  
  8.   
  9. </set>  

具体动画代码 进入

[html]
  1. <set xmlns:android="http://schemas.android.com/apk/res/android">  
  2.     <translate  
  3.         android:fromXDelta="0"  
  4.         android:fromYDelta="100%"  
  5.         android:toXDelta="0"  
  6.         android:toYDelta="0"  
  7.         android:duration="500"/>  
  8.   
  9. </set>  

activity代码

[java]
  1. @BindView(R.id.ts_test)  
  2.     TextSwitcher ts_test;  
  3.   
  4.     String[] str={  
  5.             "第一个",  
  6.             "第二个",  
  7.             "第三个",  
  8.             "第四个"  
  9.     };  
  10.     int cur;  
  11.   
  12.     @Override  
  13.     protected void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.activity_swicher);  
  16.   
  17.         ButterKnife.bind(this);  
  18.   
  19.         ts_test.setFactory(new ViewSwitcher.ViewFactory() {  
  20.             @Override  
  21.             public View makeView() {  
  22.                 final TextView tv=new TextView(SwicherTestActivity.this);  
  23.                 tv.setTextSize(25);  
  24.                 tv.setTextColor(Color.RED);  
  25.                 tv.setGravity(Gravity.CENTER);  
  26.                 //设置点击事件  
  27.                 tv.setOnClickListener(new View.OnClickListener() {  
  28.                     @Override  
  29.                     public void onClick(View v) {  
  30.                         Toast.makeText(SwicherTestActivity.this,cur+tv.getText().toString(),Toast.LENGTH_SHORT).show();  
  31.                     }  
  32.                 });  
  33.                 return tv;  
  34.             }  
  35.         });  
  36.     }  
  37.   
  38.     public void next(View view){  
  39.         //切换到下一内容,有动画  
  40.         ts_test.setText(str[cur++%str.length]);  
  41.         //设置立刻生效当前的内容,没有动画展示.  
  42. //        ts_test.setCurrentText(str[cur++%str.length]);  
  43.     }