只需要以下几个简单步骤:
1) 通过findViewById() 方法获取TextSwitcher 对象的引用switcher,当然也可以直接在代码中构造该对象。
2) 通过switcher.setFactory() 方法指定TextSwitcher 的View-Factory。
3)通过switcher.setInAnimation() 方法设置换入动画效果。
4)通过switcher.setOutAnimation() 方法设置换出动画效果。
1) 通过findViewById() 方法获取TextSwitcher 对象的引用switcher,当然也可以直接在代码中构造该对象。
2) 通过switcher.setFactory() 方法指定TextSwitcher 的View-Factory。
3)通过switcher.setInAnimation() 方法设置换入动画效果。
4)通过switcher.setOutAnimation() 方法设置换出动画效果。
TextSwitcher 的工作原理是:首先通过ViewFactory 创建两个用于在TextSwitcher 中切换的视图, 每当调用setText() 方法时,TextSwitcher 首先移除当前视图并显示setOutAnimation() 方法设置的动画,然后并将另一个视图切换进来,并显示setInAnimation()方法设置的动画。使用方法如下所示:
private TextSwitcher mTextSwitcher
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Animation in = AnimationUtils.loadAnimation(this,
android.R.anim.fade_in);
Animation out = AnimationUtils.loadAnimation(this,
android.R.anim.fade_out);
mTextSwitcher = (TextSwitcher) findViewById(R.id.your_textview);
mTextSwitcher.setFactory(new ViewFactory() {
@Override
public View makeView() {
TextView t = new TextView(YourActivity.this);
t.setGravity(Gravity.CENTER);
return t;
}
});
mTextSwitcher.setInAnimation(in);
mTextSwitcher.setOutAnimation(out);
} }