TextSwitcher--文本切换器
生活随笔
收集整理的這篇文章主要介紹了
TextSwitcher--文本切换器
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
圖片的切換可以使用ImageSwitcher實現,文本的切換動畫也是有一個叫TextSwitcher的類可以做到,他們都繼承ViewSwitcher類。
ViewSwitcher 僅僅包含子類型TextView。TextSwitcher被用來使屏幕上的label產生動畫效果。每當setText(CharSequence)被調用時,TextSwitcher使用動畫方式將當前的文字內容消失并顯示新的文字內容。
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); } } }
總結
以上是生活随笔為你收集整理的TextSwitcher--文本切换器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 前序-中序-后序-非递归-实现
- 下一篇: Sharepoint学习笔记---如何找