TextSwitcher的Java Doc是这样描述自己的:
Specialized ViewSwitcher that contains only children of type TextView. A TextSwitcher is useful to animate a label on screen. Whenever setText(CharSequence) is called, TextSwitcher animates the current text out and animates the new text in.
由此可知,TextSwitcher:
- 有个TextView子视图
- 在文本更新时,能够让旧文本淡出,新文本淡入,从而呈现平滑切换的动画效果
<TextSwitcher
android:id="@+id/ts"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginTop="20dp"
android:layout_weight="1" >
</TextSwitcher>
mTs.setFactory(new TextSwitcher.ViewFactory() {
@Override
public View makeView() {
final TextView tv = (TextView) LayoutInflater.from(
getApplicationContext()).inflate(R.layout.text, null);
return tv;
}
});
mTs.setInAnimation(AnimationUtils.loadAnimation(
getApplicationContext(), android.R.anim.fade_in));
mTs.setOutAnimation(AnimationUtils.loadAnimation(
getApplicationContext(), android.R.anim.fade_out));
之后,执行mTs.setText(txt)
来切换文本时就会产生如下效果:
注:完整代码在 GitHub
[Android] TextSwitcher -- 做什么的
原文地址:http://blog.csdn.net/programs/article/details/45000445