1.布局xml
<TextSwitcher
android:id="@+id/profileSwitcher"
android:layout_below="@id/ll_path_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/s2"
android:background="@drawable/map_top_bg_shape"
android:inAnimation="@anim/push_up_in"
android:minHeight="48dp"
android:outAnimation="@anim/push_up_out"></TextSwitcher>
2.代码
//===========================================================
@InjectView(R.id.profileSwitcher)
TextSwitcher textSwitcher;
private BitHandler bitHandler;
private String[] strings={"text00001","text00002"};
private int index = 0;
private void test(){
textSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
TextView textView = new TextView(mContext);
textView.setSingleLine();
textView.setTextSize(15);
textView.setTextColor(Color.parseColor("#ff0000"));
textView.setEllipsize(TextUtils.TruncateAt.END);
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT
);
lp.gravity = Gravity.CENTER;
textView.setLayoutParams(lp);
return textView;
}
});
bitHandler = new BitHandler();
new myThread().start();
}
class BitHandler extends Handler {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
textSwitcher.setText(strings[index]);
index++;
if (index == strings.length) {
index = 0;
}
}
}
private class myThread extends Thread {
@Override
public void run() {
super.run();
while (index < strings.length) {
try {
synchronized (this) {
bitHandler.sendEmptyMessage(0);
this.sleep(2000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
3.anim
push_up_in
<?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>
push_up_out
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false"
android:zAdjustment="bottom">
<translate
android:duration="400"
android:fromYDelta="0"
android:interpolator="@android:anim/accelerate_interpolator"
android:toYDelta="-100%"
/>
<alpha
android:duration="400"
android:fromAlpha="1.0"
android:toAlpha="0.0" />
</set>
实现效果: