本文轉載自【http://blog.csdn.net/u014369799/article/details/50337229】
實現功能:用TextSwitcher實現文本自動垂直滾動,類似淘寶首頁廣告條。
實現效果:
注意:由于網上橫向滾動的例子比較多,所以這里通過垂直的例子演示。
實現步驟:1、extends TextSwitcher implements ViewFactory
2、重寫makeView(),在里面返回一個TextView
3、對TextSwitcher做初始化設置:setFactory、setInAnimation、setOutAnimation
4、給TextSwitcher設置要滾動的文本內容
5、使用Timer進行定時發送消息給Handler,handler收到消息之后,取出下一個要顯示的文本,然后執行內容的切換。
上代碼:
[java]?view plaincopy
package?com.example.testtextview;?? import?java.util.Timer;?? import?java.util.TimerTask;?? import?android.content.Context;?? import?android.os.Handler;?? import?android.os.Message;?? import?android.util.AttributeSet;?? import?android.view.View;?? import?android.view.animation.AnimationUtils;?? import?android.widget.TextSwitcher;?? import?android.widget.TextView;?? import?android.widget.ViewSwitcher.ViewFactory;?? ?? ? ? ? ? ? ? ?? public?class?TextSwitchView?extends?TextSwitcher?implements?ViewFactory{?? ????private?int?index=?-1;?? ????private?Context?context;?? ????private?Handler?mHandler?=?new?Handler(){?????? <span?style="white-space:pre">????</span>public?void?handleMessage(Message?msg)?{?????? ????????????switch?(msg.what)?{?????? ????????????case?1:?????? ????????????????index?=?next();??? ????????????????updateText();???? ????????????????break;?????? ????????????}?????? ????????};?????? ????};??? ????private?String?[]?resources={???? ????????????"靜夜思",?? ????????????"床前明月光","疑是地上霜",???? ????????????"舉頭望明月",???? ????????????"低頭思故鄉"?? ????};?? ????private?Timer?timer;??? ????public?TextSwitchView(Context?context)?{?? ????????super(context);?? ????????this.context?=?context;?? ????????init();??? ????}?? ????public?TextSwitchView(Context?context,?AttributeSet?attrs)?{?? ????????super(context,?attrs);?? ????????this.context?=?context;?? ????????init();????? ????}?? ????private?void?init()?{?? ????????if(timer==null)?? ????????????timer?=?new?Timer();?????? ????????this.setFactory(this);???? ????????this.setInAnimation(AnimationUtils.loadAnimation(context,?R.anim.in_animation));???? ????????this.setOutAnimation(AnimationUtils.loadAnimation(context,?R.anim.out_animation));?? ????}?? ????public?void?setResources(String[]?res){?? ????????this.resources?=?res;?? ????}?? ????public?void?setTextStillTime(long?time){?? ????????if(timer==null){?? ????????????timer?=?new?Timer();?????? ????????}else{?? ????????????timer.scheduleAtFixedRate(new?MyTask(),?1,?time);?? ????????}?? ????}?? ????private?class?MyTask?extends?TimerTask{?????? ????????@Override?????? ????????public?void?run()?{?????? ????????????mHandler.sendEmptyMessage(1);?? ????????}????????? ????}???? ????private?int?next(){???? ????????int?flag?=?index+1;???? ????????if(flag>resources.length-1){???? ????????????flag=flag-resources.length;???? ????????}???? ????????return?flag;???? ????}???? ????private?void?updateText(){???? ????????this.setText(resources[index]);???? ????}?? ????@Override?? ????public?View?makeView()?{?? ????????TextView?tv?=new?TextView(context);???? ????????return?tv;???? ????}?? }?? </span></span>??
in_animation.xml
[java]?view plaincopy
<?xml?version="1.0"?encoding="utf-8"?>?? <set?xmlns:android="http://schemas.android.com/apk/res/android"?>?? ????<translate?? ????????android:duration="2000"?? ????????android:fromYDelta="100%p"?? ????????android:toYDelta="0%p"?/>?? </set>??
out_animation.xml
[java]?view plaincopy
<?xml?version="1.0"?encoding="utf-8"?>?? <set?xmlns:android="http://schemas.android.com/apk/res/android"?>?? ????<translate?? ????????android:duration="2000"?? ????????android:fromYDelta="0%p"?? ????????android:toYDelta="-100%p"?/>?? </set>??
主程序調用:
[java]?view plaincopy
TextSwitchView?tsv?=?(TextSwitchView)?findViewById(R.id.tsv);?? String?[]?res={???? ????????????"靜夜思",?? ????????????"床前明月光","疑是地上霜",???? ????????????"舉頭望明月",???? ????????????"低頭思故鄉"?? };?? tsv.setResources(res);?? tsv.setTextStillTime(5000);??
注意事項:
1.在布局文件使用該自定義控件時候,需要修改下全路徑名為你項目該控件的全路徑名,這里我是
<com.example.testtextview.TextSwitchView/>
2.使用時候直接先調用setResources設置內容,再調用setTextStillTime設置文本停留時間,并自動啟動。
3.如需修改內容,只要直接調用setResources就好,不要重復調用setTextStillTime
代碼解析:
1、ViewFactory:,是一個視圖工廠。它需要實現makeView()去返回你要的一個視圖,這里是實現文本滾動,所以直接返回一個TextView,這里順帶修改TextView的一些屬性,比如文字大小等。
2、setFactory:看下源碼的解釋:Sets the factory used to create the two views between which the?ViewSwitcher will flip.
實際上它幫我們創建了兩個view,然后通過ViewSwitcher幫我們實現了翻轉。
3、重點來了,剛剛提到ViewSwitcher其實只是幫我們實現視圖的切換,然而,視圖的切換的形式動畫,是可以由你自己來定的。
this.setInAnimation(AnimationUtils.loadAnimation(context, R.anim.in_animation)); ?//視圖進來時候的動畫
this.setOutAnimation(AnimationUtils.loadAnimation(context, R.anim.out_animation));//視圖出去時候的動畫
如果你不想垂直滾動,想實現水平滾動,這里直接修改動畫就可以了。
4、動畫分析:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:Android="http://schemas.android.com/apk/res/android" >
? ? <translate
? ? ? ? android:duration="2000"//動畫的持續時間,如果覺得文本滾動過程太慢,可以修改這里的時間
? ? ? ? android:fromYDelta="100%p"//Y位置的起點,這里要先清楚一點,文本顯示在正中間的時候是0%p,由于android的坐標系中,y軸往下為正。所以文本進來的時候,其實是從100%p->0%p
? ? ? ? android:toYDelta="0%p" />
</set>
另一個動畫就不解釋了,原理一樣,從0%p->-100%p,自然它就出去了
5、剩下就是通過Timer去調用Handler,然后執行this.setText(resources[index]); ?去修改文本內容而已了。文本內容修改完,滾進跟滾出的動畫剛才已經解釋了。收工。
總結
以上是生活随笔為你收集整理的TextSwitcher实现文本自动垂直滚动的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。