TextSwitcher实现文本自动垂直滚动
实现功能:用TextSwitcher实现文本自动垂直滚动,类似淘宝首页广告条。
实现效果:
注意:由于网上横向滚动的例子比较多,所以这里通过垂直的例子演示。
实现步骤:1、extends TextSwitcher implements ViewFactory
2、重写makeView(),在里面返回一个TextView
3、对TextSwitcher做初始化设置:setFactory、setInAnimation、setOutAnimation
4、给TextSwitcher设置要滚动的文本内容
5、使用Timer进行定时发送消息给Handler,handler收到消息之后,取出下一个要显示的文本,然后执行内容的切换。
上代码:
- 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;
 - /**
 - * @author (●—●)
 - *
 - * @data 2015-12-15下午3:36:00
 - *
 - * @describe
 - */
 - 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(); //更新TextSwitcherd显示内容;
 - 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);//每3秒更新
 - }
 - }
 - 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
- <?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
- <?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>
 
主程序调用:
- TextSwitchView tsv = (TextSwitchView) findViewById(R.id.tsv);
 - String [] res={
 - "静夜思",
 - "床前明月光","疑是地上霜",
 - "举头望明月",
 - "低头思故乡"
 - };
 - tsv.setResources(res);
 - tsv.setTextStillTime(5000);
 
注意事项:
1.在布局文件使用该自定义控件时候,需要修改下全路径<com.example.testtextview.TextSwitchView/>
2.使用时候直接先调用setTextStillTime设置文本停留时间,并自动启动。
setResources就好,不要重复调用代码解析:
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]); 去修改文本内容而已了。文本内容修改完,滚进跟滚出的动画刚才已经解释了。收工。
                    
                
                
            
        
浙公网安备 33010602011771号