当前位置:   article > 正文

Android TextSwitcher - 文本切换器_android textswitcher两个textview切换

android textswitcher两个textview切换

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

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

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

Android-TextSwitcher



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/812795
推荐阅读
相关标签
  

闽ICP备14008679号