在layout布局文件中声明TextSwitcher
<!--inAnimation 进来的动画-->
<!--outAnimation 出去的动画-->
<TextSwitcher
android:id="@+id/follow_textSwitcher_up"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inAnimation="@anim/push_up_in"
android:outAnimation="@anim/push_up_out">
</TextSwitcher>
位移的动画
inAnimation的动画,outAnimation的动画修改fromYDelta的值即可
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false"
android:zAdjustment="top">
<translate
android:duration="400"
android:fromYDelta="100%"
android:interpolator="@android:anim/accelerate_interpolator"
android:toYDelta="0"
/>
<alpha
android:duration="400"
android:fromAlpha="0.0"
android:toAlpha="1.0"/>
</set>
添加TextSwitcher的主体内容
TextSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
//这里 用来创建内部的视图,这里创建TextView,用来显示文字
@Override
public View makeView() {
final TextView tv = new TextView(getContext());
//设置文字大小
tv.setTextSize(UIUtils.dip2px(15));
//设置文字 颜色
tv.setTextColor(Color.BLACK);
tv.setSingleLine();
tv.setEllipsize(TextUtils.TruncateAt.END);
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp.gravity = Gravity.CENTER;
tv.setLayoutParams(lp);
//内容的点击事件
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ToastUtils.showToast(tv.getText().toString());
}
});
return tv;
}
});
Handler设置循环
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case NEWS_MESSAGE_TEXTVIEW:
index++;
TextSwitcher.setText(mStrings.get(index%mStrings.size()));
if (index == mStrings.size()) {
index = 0;
}
//3秒循环一次
handler.sendEmptyMessageDelayed(NEWS_MESSAGE_TEXTVIEW,3000);
break;
default:
break;
}
}
};
..........................
//3秒后启动循环
handler.sendEmptyMessageDelayed(NEWS_MESSAGE_TEXTVIEW,3000);