TextSwitcher-------->实现文本自动垂直滚动

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

实现效果:

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

实现步骤:1、extends TextSwitcher implements ViewFactory

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

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

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

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

上代码:

[java]  view plain  copy
  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

[java]  view plain  copy
  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

[java]  view plain  copy
  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>  

主程序调用:

[java]  view plain  copy
  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.使用时候直接先调用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]);  去修改文本内容而已了。文本内容修改完,滚进跟滚出的动画刚才已经解释了。收工。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在最新版的Android Studio中实现TextSwitcher自动上下滚动效果,你可以使用Handler和Runnable来实现定时切换文本的功能。以下是实现步骤: 1. 在XML布局文件中,添加TextSwitcher到你的布局中: ```xml <TextSwitcher android:id="@+id/textSwitcher" android:layout_width="wrap_content" android:layout_height="wrap_content" android:inAnimation="@android:anim/slide_in_bottom" android:outAnimation="@android:anim/slide_out_top" /> ``` 2. 在Java代码中,获取TextSwitcher的实例,并设置相应的属性: ```java TextSwitcher textSwitcher = findViewById(R.id.textSwitcher); // 设置TextSwitcher文本切换动画 textSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left)); textSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right)); // 设置TextSwitcher文本颜色、字体大小等属性 textSwitcher.setTextColor(Color.BLACK); textSwitcher.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16); ``` 3. 创建一个Handler对象和一个Runnable对象,用于定时切换文本: ```java Handler handler = new Handler(); Runnable runnable = new Runnable() { int counter = 0; @Override public void run() { counter++; if (counter % 2 == 0) { textSwitcher.setText("First Text"); } else { textSwitcher.setText("Second Text"); } // 设置延迟时间(这里设置为3秒) handler.postDelayed(this, 3000); } }; ``` 4. 在Activity的onResume()方法中启动Runnable,开始自动切换文本: ```java @Override protected void onResume() { super.onResume(); handler.postDelayed(runnable, 0); } ``` 5. 在Activity的onPause()方法中停止Runnable,以防止在后台持续切换文本: ```java @Override protected void onPause() { super.onPause(); handler.removeCallbacks(runnable); } ``` 这样,TextSwitcher将会在 "First Text" 和 "Second Text" 之间自动上下滚动,并且会显示滑入和滑出的动画效果。你可以根据自己的需求修改文本和延迟时间。希望这对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值