Android开发:TextSwitcher--文本切换器


图片的切换可以使用ImageSwitcher实现,文本的切换动画也是有一个叫TextSwitcher的类可以做到,他们都继承ViewSwitcher类。

ViewSwitcher 仅仅包含子类型TextView。TextSwitcher被用来使屏幕上的label产生动画效果。每当setText(CharSequence)被调用时,TextSwitcher使用动画方式将当前的文字内容消失并显示新的文字内容。

  1. package com.shao.act;  
  2.   
  3. import java.util.Timer;  
  4. import java.util.TimerTask;  
  5.   
  6. import Android.app.Activity;  
  7. import android.os.Bundle;  
  8. import android.os.Handler;  
  9. import android.os.Message;  
  10. import android.view.View;  
  11. import android.view.View.OnClickListener;  
  12. import android.view.animation.AnimationUtils;  
  13. import android.widget.Button;  
  14. import android.widget.TextSwitcher;  
  15. import android.widget.TextView;  
  16. import android.widget.ViewSwitcher.ViewFactory;  
  17.   
  18. public class TextSwitcherActivity extends Activity implements ViewFactory{  
  19.     /** Called when the activity is first created. */  
  20.     TextSwitcher switcher;  
  21.     Handler handler;  
  22.     String [] resources={  
  23.             " ","身是菩提树,",  
  24.             "心如明镜台,",  
  25.             "时时勤拂拭,",  
  26.             "勿使惹尘埃。"  
  27.     };  
  28.      private Handler mHandler = new Handler(){    
  29.            
  30.             public void handleMessage(Message msg) {    
  31.                 switch (msg.what) {    
  32.                 case 1:    
  33.                     id = next(); //更新Id值   
  34.                     updateText();  //更新TextSwitcherd显示内容;   
  35.                     break;    
  36.                 }    
  37.             };    
  38.         };    
  39.     int id= 0//resources 数组的Id;   
  40.     @Override  
  41.     public void onCreate(Bundle savedInstanceState) {  
  42.         super.onCreate(savedInstanceState);  
  43.         setContentView(R.layout.main);  
  44.         init();  
  45.         Timer timer = new Timer();    
  46.         timer.scheduleAtFixedRate(new MyTask(), 13000);//每3秒更新   
  47.     }  
  48.    private void init(){  
  49.        switcher = (TextSwitcher) findViewById(R.id.switcher);  
  50.        switcher.setFactory(this);  
  51.        switcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));  
  52.        switcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));  
  53.    }  
  54.  private int next(){  
  55.       
  56.      int flag = id+1;  
  57.      if(flag>resources.length-1){  
  58.          flag=flag-resources.length;  
  59.      }  
  60.      return flag;  
  61.  }  
  62.  private void updateText(){  
  63.      switcher.setText(resources[id]);  
  64.  }  
  65.     @Override  
  66.     public View makeView() {  
  67.         // TODO Auto-generated method stub   
  68.         TextView tv =new TextView(this);  
  69.         tv.setText(resources[id]);  
  70.         return tv;  
  71.     }  
  72.      private class MyTask extends TimerTask{    
  73.             @Override    
  74.             public void run() {    
  75.                 Message message = new Message();    
  76.                 message.what = 1;    
  77.                 mHandler.sendMessage(message);    
  78.                     
  79.             }       
  80.         }    
  81. }  

相关内容