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子视图
- 在文本更新时,能够让旧文本淡出,新文本淡入,从而呈现平滑切换的动画效果
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)
来切换文本时就会产生如下效果: