使用TextSwitcher和ImageSwitcher实现平滑过渡

来源:互联网 发布:海关数据买卖合法吗 编辑:程序博客网 时间:2024/05/17 04:05

如果需要在TextView或者ImageView中循环浏览信息,使用TextSwitcher是比较好的选择,使用的机制和TextView类似,当需要更改所需的内容时,能够添加动画效果以避免生硬的内容切换方式,就可以使用它。当调用它的相关方法时,变会以动画的方式换出当前文本,并换入新的文本,只需要以下几个步骤:

(1)通过findViewById()方法获取TextSwitcher对象的应用switcher,当然也可以直接在代码中构造改对象。

(2)通过switcher.setFactory()方法指定TextSwitcher的ViewFactory。

(3)通过switcher.setInAnimation()方法设置换入动画效果。

(4)通过switcher.setOutAnimation()方法设置换出动画效果。

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.textview);mTextSwitcher.setFactory(new ViewFactory() {   @Override   public View makeView() {      TextView t = new TextView(MainActivity.this);      t.setGravity(Gravity.CENTER);      return t;   }});mTextSwitcher.setInAnimation(in);mTextSwitcher.setOutAnimation(out);
ImageSwitcher的原理也是一样的,只是一个是文本,一个是图片。


0 0
原创粉丝点击