這也是從範例中挖出來的,使用 ListView 來顯示清單時,預設的顯示方式是瞬間顯示,沒有任何的動畫,你可以使用 anim + LayoutAnimationController ,讓你的清單顯示增加質感,使用方式相當簡單,當已經處理完 ListView 的建立後,再加上 anim 和 LayoutAnimationController 即可,如下
1: void listShowAnim(){
   2:          
3: AnimationSet set = new AnimationSet(true);
   4:   
   5:          Animation animation;
   6:          
7: animation = new TranslateAnimation(-300.0f,0.0f,0.0f,0.0f);
   8:          animation.setDuration(1000);
9: set.addAnimation(animation);
  10:          
11: // Animation animation;
12: // animation = new AlphaAnimation(0.0f, 1.0f);
13: // animation.setDuration(500);
14: // set.addAnimation(animation);
15: //
16: // animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
17: // Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
18: // -1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
19: // animation.setDuration(100);
20: // set.addAnimation(animation);
  21:   
22: //every sublist action move, 0.0f: action together
23: LayoutAnimationController controller = new LayoutAnimationController(
24: set, 0.0f);
  25:   
  26:          lv.setLayoutAnimation(controller);
  27:          
  28:      }
第 1 行為動畫方法的名子,以我的範例而言,它放在 initXml() 之後呼叫
第 3 行建立 AnimationSet set 如果想使用多種動畫就使用 AnimationSet ,只有1種的話使用 Animation 即可
第 5 行到第 8 行就是第 1 個動畫的建立,單純的移位動作
第 9 行把動畫加入 set
第 11 ~ 22行的註解就是多種動畫的示範
第 23 行使用 LayoutAnimationController 可以決定 ListView 中的清單是否一起動作, 0.0f 代表一起動作,如果是 1.0f 的話代表清單會以 1 秒為單位依序動作
第 26 行執行動畫,該動畫為從左邊移位進入畫面置中位置
最後把它放到 initXml() 後呼叫
1: public void onCreate(Bundle savedInstanceState){
   2:          super.onCreate(savedInstanceState);
   3:          
   4:          setContentView(R.layout.tabwidget4);
   5:          
   6:          initXml();
   7:          
   8:          listShowAnim();
   9:      }
結果為
清單移動中...
清單移動中...





 Posted in:  
0 意見:
張貼留言