欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 上下滚动TextSwitcher实例详解

程序员文章站 2023-11-23 22:52:52
android 上下滚动textswitcher实例详解 1.在activity中需要代码声明 textswitcher = (textswitcher)fin...

android 上下滚动textswitcher实例详解

1.在activity中需要代码声明

textswitcher = (textswitcher)findviewbyid(r.id.text_switcher); 
    textswitcher.setfactory(new viewfactory() { 
       
      @override 
      public view makeview() { 
        textview tv = new  textview(mainactivity.this); 
        tv.settextsize(typedvalue.complex_unit_dip, 16.0f); 
        tv.settextcolor(color.red); 
        return tv; 
      } 
    }); 
     
    textswitcher.setinanimation(animationutils.loadanimation(this, r.anim.anim_in)); 
    textswitcher.setoutanimation(animationutils.loadanimation(this, r.anim.anim_out)); 

2.两个anim动画xml

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
   android:fillafter="true"  
  android:shareinterpolator="false"  android:zadjustment="top"> 
  <translate  
    android:duration="3000"  
    android:fromydelta="100%p"  
    android:toydelta="0" /> 
</set> 
<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
  android:fillafter="true"  
  android:shareinterpolator="false"  android:zadjustment="top"> 
   
   <translate  
    android:duration="3000"  
    android:fromydelta="0"  
    android:toydelta="-100%p" />  
</set> 
<style name="pop_anim"> 
    <item name="android:windowenteranimation">@anim/anim_in</item> 
  <item name="android:windowexitanimation">@anim/anim_out</item> 
  </style> 

3.用线程或者定时器实现循环翻动。

thread t = new thread(new runnable() { 
     
    @override 
    public void run() { 
      while (!flag) { 
        message msg = new message(); 
        msg.what = 1; 
        msg.obj = getitem[i]; 
        handler.sendmessage(msg); 
        if (i== 2) { 
          i = 0; 
        } 
        try { 
          t.sleep(3000); 
          i++; 
           
        } catch (interruptedexception e) { 
          // todo auto-generated catch block 
          e.printstacktrace(); 
        } 
      } 
       
    } 

4.hanlder更新ui

private handler handler = new handler(){ 
    public void handlemessage(android.os.message msg) { 
       
      textswitcher.settext((string)msg.obj); 
       
      super.handlemessage(msg); 
    }; 
  }; 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!