android文本切换器

图片的切换可以使用ImageSwitcher来实现,同样文本的切换动画也可以使用Android TextSwitcher类,他们都继承ViewSwitcher类。

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

[java]  view plain copy
  1. <span style="font-size:16px;">package com.android.study;   
  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.     private Handler mHandler = new Handler() {   
  24.     
  25.         public void handleMessage(Message msg) {   
  26.             switch (msg.what) {   
  27.             case 1:   
  28.                 id = next(); // 更新Id值   
  29.                 updateText(); // 更新TextSwitcherd显示内容;   
  30.                 break;   
  31.             }   
  32.         };   
  33.     };   
  34.     int id = 0// resources 数组的Id;   
  35.     
  36.     @Override  
  37.     public void onCreate(Bundle savedInstanceState) {   
  38.         super.onCreate(savedInstanceState);   
  39.         setContentView(R.layout.main);   
  40.         init();   
  41.         Timer timer = new Timer();   
  42.         timer.scheduleAtFixedRate(new MyTask(), 13000);// 每3秒更新   
  43.     }   
  44.     
  45.     private void init() {   
  46.         switcher = (TextSwitcher) findViewById(R.id.switcher);   
  47.         switcher.setFactory(this);   
  48.         switcher.setInAnimation(AnimationUtils.loadAnimation(this,   
  49.                 android.R.anim.fade_in));   
  50.         switcher.setOutAnimation(AnimationUtils.loadAnimation(this,   
  51.                 android.R.anim.fade_out));   
  52.     }   
  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.     
  63.     private void updateText() {   
  64.         switcher.setText(resources[id]);   
  65.     }   
  66.     
  67.     @Override  
  68.     public View makeView() {   
  69.         // TODO Auto-generated method stub   
  70.         TextView tv = new TextView(this);   
  71.         tv.setText(resources[id]);   
  72.         return tv;   
  73.     }   
  74.     
  75.     private class MyTask extends TimerTask {   
  76.         @Override  
  77.         public void run() {   
  78.             Message message = new Message();   
  79.             message.what = 1;   
  80.             mHandler.sendMessage(message);   
  81.     
  82.         }   
  83.     }   
  84. }</span>  

Android-TextSwitcher

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值