TextSwitcher 字面理解是文字交换器,是ViewSwitcher的子类,从ViewSwitcher来看,是View交换器,TextSwitcher继承自ViewSwitcher,显然是交换TextView。
应用分为三步:
1.得到 TextSwitcher 实例对象
TextSwitcher switcher = (TextSwitcher) findViewById(R.id.textSwitcher);
2.为switcher指定ViewSwitcher.ViewFactory工厂,该工厂会产生出转换时需要的View
switcher.setFactory(this);
3.为switcher设定显示的内容,该方法执行,就会切换到下个View
switcher.setText(String.valueOf(new Random().nextInt()));
其中 要实现ViewSwitcher.ViewFactory中的makeView()方法
// 重写 ViewSwitcher.ViewFactory 的 makeView()方法,返回一个 View,TextSwitcher 交换时使用
@Override
public View makeView() {
TextView textView = new TextView(this);
textView.setTextSize(36);
return textView;
}
如果不适用ViewSwitcher.ViewFactory,也可以使用下面的方式代替
//如果不用switcher.setFactory()方法设置转换时的View,也可以调用两次switcher.addView(view,index,params);
//其中view为要切换的View,index为索引,params是添加时的宽,高参数
// TextView textView1 = new TextView(this);
// textView1.setTextSize(36);
// textView1.setTextColor(Color.RED);
// TextView textView2 = new TextView(this);
// textView2.setTextSize(36);
// textView2.setTextColor(Color.YELLOW);
// switcher.addView(textView1, 0,new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
// switcher.addView(textView2, 1,new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
实现功能:用TextSwi
tcher实现文本自动垂直滚动,类似淘宝首页广告条。
注意:由于网上横向滚动的例子比较多,所以这里通过垂直的例子演示。
实现步骤:1、extends TextSwitcher implements ViewFactory
2、重写makeView(),在里面返回一个TextView
3、对TextSwitcher做初始化设置:setFactory、setInAnimation、setOutAnimation
4、给TextSwitcher设置要滚动的文本内容
5、使用Timer进行定时发送消息给Handler,handler收到消息之后,取出下一个要显示的文本,然后执行内容的切换。
上代码:
public class TextSwitchView extends TextSwitcher implements ViewFactory{
private int index= -1;
private Context context;
private Handler mHandler = new Handler(){
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;
}
}
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.在布局文件使用该自定义控件时候,需要修改下全路径名为你项目该控件的全路径名
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]); 去修改文本内容而已了。文本内容修改完,滚进跟滚出的动画刚才已经解释了