TextSwitcher--文本切换器

 转至:

http://blog.csdn.net/shaojie519/article/details/6713187

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

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

view plain copy to clipboard print ?
  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. }  
package com.shao.act; import java.util.Timer; import java.util.TimerTask; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.view.animation.AnimationUtils; import android.widget.Button; import android.widget.TextSwitcher; import android.widget.TextView; import android.widget.ViewSwitcher.ViewFactory; public class TextSwitcherActivity extends Activity implements ViewFactory{ /** Called when the activity is first created. */ TextSwitcher switcher; Handler handler; String [] resources={ " ","身是菩提树,", "心如明镜台,", "时时勤拂拭,", "勿使惹尘埃。" }; private Handler mHandler = new Handler(){ public void handleMessage(Message msg) { switch (msg.what) { case 1: id = next(); //更新Id值 updateText(); //更新TextSwitcherd显示内容; break; } }; }; int id= 0; //resources 数组的Id; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); Timer timer = new Timer(); timer.scheduleAtFixedRate(new MyTask(), 1, 3000);//每3秒更新 } private void init(){ switcher = (TextSwitcher) findViewById(R.id.switcher); switcher.setFactory(this); switcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); switcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out)); } private int next(){ int flag = id+1; if(flag>resources.length-1){ flag=flag-resources.length; } return flag; } private void updateText(){ switcher.setText(resources[id]); } @Override public View makeView() { // TODO Auto-generated method stub TextView tv =new TextView(this); tv.setText(resources[id]); return tv; } private class MyTask extends TimerTask{ @Override public void run() { Message message = new Message(); message.what = 1; mHandler.sendMessage(message); } } }
view plain copy to clipboard print ?
  1.   
  
 

你可能感兴趣的:(timer,Class,resources)