zoukankan      html  css  js  c++  java
  • TextSwitcher实现文本自动垂直滚动

    实现功能:用TextSwitcher实现文本自动垂直滚动,类似淘宝首页广告条。

    实现效果:

    注意:由于网上横向滚动的例子比较多,所以这里通过垂直的例子演示。

    实现步骤:1、extends TextSwitcher implements ViewFactory

    2、重写makeView(),在里面返回一个TextView

    3、对TextSwitcher做初始化设置:setFactory、setInAnimation、setOutAnimation

    4、给TextSwitcher设置要滚动的文本内容

    5、使用Timer进行定时发送消息给Handler,handler收到消息之后,取出下一个要显示的文本,然后执行内容的切换。

    上代码:

    1. package com.example.testtextview;  
    2. import java.util.Timer;  
    3. import java.util.TimerTask;  
    4. import android.content.Context;  
    5. import android.os.Handler;  
    6. import android.os.Message;  
    7. import android.util.AttributeSet;  
    8. import android.view.View;  
    9. import android.view.animation.AnimationUtils;  
    10. import android.widget.TextSwitcher;  
    11. import android.widget.TextView;  
    12. import android.widget.ViewSwitcher.ViewFactory;  
    13.   
    14. /** 
    15.  * @author (●—●) 
    16.  * 
    17.  * @data 2015-12-15下午3:36:00 
    18.  * 
    19.  * @describe  
    20.  */  
    21. public class TextSwitchView extends TextSwitcher implements ViewFactory{  
    22.     private int index= -1;  
    23.     private Context context;  
    24.     private Handler mHandler = new Handler(){      
    25. <span style="white-space:pre">    </span>public void handleMessage(Message msg) {      
    26.             switch (msg.what) {      
    27.             case 1:      
    28.                 index = next(); //取得下标值    
    29.                 updateText();  //更新TextSwitcherd显示内容;    
    30.                 break;      
    31.             }      
    32.         };      
    33.     };   
    34.     private String [] resources={    
    35.             "静夜思",  
    36.             "床前明月光","疑是地上霜",    
    37.             "举头望明月",    
    38.             "低头思故乡"  
    39.     };  
    40.     private Timer timer; //  
    41.     public TextSwitchView(Context context) {  
    42.         super(context);  
    43.         this.context = context;  
    44.         init();   
    45.     }  
    46.     public TextSwitchView(Context context, AttributeSet attrs) {  
    47.         super(context, attrs);  
    48.         this.context = context;  
    49.         init();     
    50.     }  
    51.     private void init() {  
    52.         if(timer==null)  
    53.             timer = new Timer();      
    54.         this.setFactory(this);    
    55.         this.setInAnimation(AnimationUtils.loadAnimation(context, R.anim.in_animation));    
    56.         this.setOutAnimation(AnimationUtils.loadAnimation(context, R.anim.out_animation));  
    57.     }  
    58.     public void setResources(String[] res){  
    59.         this.resources = res;  
    60.     }  
    61.     public void setTextStillTime(long time){  
    62.         if(timer==null){  
    63.             timer = new Timer();      
    64.         }else{  
    65.             timer.scheduleAtFixedRate(new MyTask(), 1, time);//每3秒更新    
    66.         }  
    67.     }  
    68.     private class MyTask extends TimerTask{      
    69.         @Override      
    70.         public void run() {      
    71.             mHandler.sendEmptyMessage(1);  
    72.         }         
    73.     }    
    74.     private int next(){    
    75.         int flag = index+1;    
    76.         if(flag>resources.length-1){    
    77.             flag=flag-resources.length;    
    78.         }    
    79.         return flag;    
    80.     }    
    81.     private void updateText(){    
    82.         this.setText(resources[index]);    
    83.     }  
    84.     @Override  
    85.     public View makeView() {  
    86.         TextView tv =new TextView(context);    
    87.         return tv;    
    88.     }  
    89. }  
    90. </span></span>  

    in_animation.xml

    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <set xmlns:android="http://schemas.android.com/apk/res/android" >  
    3.     <translate  
    4.         android:duration="2000"  
    5.         android:fromYDelta="100%p"  
    6.         android:toYDelta="0%p" />  
    7. </set>  


    out_animation.xml

    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <set xmlns:android="http://schemas.android.com/apk/res/android" >  
    3.     <translate  
    4.         android:duration="2000"  
    5.         android:fromYDelta="0%p"  
    6.         android:toYDelta="-100%p" />  
    7. </set>  


    主程序调用:

    1. TextSwitchView tsv = (TextSwitchView) findViewById(R.id.tsv);  
    2. String [] res={    
    3.             "静夜思",  
    4.             "床前明月光","疑是地上霜",    
    5.             "举头望明月",    
    6.             "低头思故乡"  
    7. };  
    8. tsv.setResources(res);  
    9. 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]);  去修改文本内容而已了。文本内容修改完,滚进跟滚出的动画刚才已经解释了。收工。


  • 相关阅读:
    多条件查询
    BootStrap——BootStrap练习(栅格系统、组件、插件)
    BootStrap——栅格系统
    BootStrap——BootStrap插件
    BootStrap——BootStrap组件
    BootStrap——按钮、图片
    BootStrap——CSS
    BootStrap——新建BootStrap项目
    JQ——表单验证插件(validation)
    JQ——事件(表单、文档/窗口)
  • 原文地址:https://www.cnblogs.com/dubo-/p/7887202.html
Copyright © 2011-2022 走看看