TextSwitcher实现文本自动垂直滚动
来源:互联网 发布:网络基础知识教程视频 编辑:程序博客网 时间:2024/05/22 17:34
实现功能:用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]); }@Overridepublic 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.使用时候直接先调用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实现文本自动垂直滚动
- TextSwitcher-------->实现文本自动垂直滚动
- TextSwitcher实现文本自动垂直滚动
- APP实用开发——TextSwitcher实现文本自动垂直滚动
- android TextView 垂直自动滚动字幕实现TextSwitcher
- Android 实现上下滚动TextSwitcher
- android TextView 垂直自动滚动字幕实现
- android TextView 垂直自动滚动字幕实现
- android TextView 垂直自动滚动字幕实现
- android TextView 垂直自动滚动字幕实现
- android TextView 垂直自动滚动字幕实现
- android_文本垂直滚动
- TextView自动垂直滚动
- Android让文本自动滚动实现
- 实现TextView自动左右滚动文本
- 实现类似天猫列表消息自动垂直滚动效果
- 通知公告TextSwitcher自动上下滚动带点击事件
- Android TextView 垂直自动滚动
- Sqlite3开发
- Android 短信验证码自动填写
- Strongswan5.3.3与win7 l2tpoverIPsecVPN对接的配置与打印
- cmd 相关命令
- Nil is incompatible with return type ...
- TextSwitcher实现文本自动垂直滚动
- JS判断输入字符串长度(汉字算两个字符,字母数字算一个)
- 杭电ACM2033
- 解决android studio 无法更新插件的问题(某些)
- php入门基础
- OpenCV截取图像的任意形状区域,规则的图形(圆、椭圆、矩形),不规则鼠标自己选择
- window安装vagrant+virtualbox
- IFRAME的内容增高或缩减时设置其IFRAME的高度的处理方案
- 真当程序猿眼中只有美女和高薪?