Android ListView動畫實現方法

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   color   ar   java   sp   

在Android中listview是最常用的控制項之一,但是有時候我們會覺得千篇一律的listview看起來過於單調,於是就產生了listView動畫,listview載入了動畫會讓使用者體驗更好,本期就分享一些listview動畫以及實現方法,



相信大家都熟悉Android的Tween動畫,前四種動畫就是Translate,Alpha,Rotate,Scale,最後一種Rotate3d則是用了一個3D旋轉動畫工具類Rotate3dAnimation,這個類的建構函式中接收一些3D旋轉時所需用到的參數,比如旋轉開始和結束的角度,旋轉的中心點等。

LayoutAnimationController可以控制一組控制項按照規定顯示,ListView中的mListView.setLayoutAnimation相信大家都知道是用來幹什麼的了,接下來上代碼

 

[java] view plaincopy
    1. private Button button, button2, button3, button4, button5;  
    2. private ListView mListView;  
    3. private Animation animation;  
    4. private LayoutAnimationController controller;  
    5. private String[] arry = { "一", "二", "三", "四", "五", "六" };  
    6. private ArrayAdapter<String> adapter;  
    7.   
    8. protected void onCreate(Bundle savedInstanceState) {  
    9.     super.onCreate(savedInstanceState);  
    10.     setContentView(R.layout.activity_main);  
    11.     initView();  
    12.     adapter = new ArrayAdapter<String>(this,  
    13.             android.R.layout.simple_list_item_1, arry);  
    14.     mListView.setAdapter(adapter);  
    15.   
    16. }  
    17.   
    18. private void initView() {  
    19.     // TODO Auto-generated method stub  
    20.     mListView = (ListView) findViewById(R.id.list);  
    21.     button = (Button) findViewById(R.id.btn_tran);  
    22.     button.setOnClickListener(this);  
    23.     button2 = (Button) findViewById(R.id.btn_alpha);  
    24.     button2.setOnClickListener(this);  
    25.     button3 = (Button) findViewById(R.id.btn_rotate);  
    26.     button3.setOnClickListener(this);  
    27.     button4 = (Button) findViewById(R.id.btn_scale);  
    28.     button4.setOnClickListener(this);  
    29.     button5 = (Button) findViewById(R.id.rotate3d);  
    30.     button5.setOnClickListener(this);  
    31. }  
    32.   
    33. @Override  
    34. public void onClick(View arg0) {  
    35.     // LayoutAnimationController.ORDER_NORMAL; 順序顯示  
    36.     // LayoutAnimationController.ORDER_REVERSE;反顯示  
    37.     // LayoutAnimationController.ORDER_RANDOM; 隨機顯示  
    38.     switch (arg0.getId()) {  
    39.     case R.id.btn_tran:  
    40.         animation = new TranslateAnimation(-50f, 0f, 0f, 0f);  
    41.         animation.setDuration(500);  
    42.         //1f為延時  
    43.         controller = new LayoutAnimationController(animation, 1f);  
    44.         controller.setOrder(LayoutAnimationController.ORDER_NORMAL);  
    45.         mListView.setLayoutAnimation(controller);  
    46.         adapter.notifyDataSetInvalidated();  
    47.         break;  
    48.     case R.id.btn_alpha:  
    49.         animation = new AlphaAnimation(0f, 1f);  
    50.         animation.setDuration(500);  
    51.         controller = new LayoutAnimationController(animation, 1f);  
    52.         controller.setOrder(LayoutAnimationController.ORDER_NORMAL);  
    53.         mListView.setLayoutAnimation(controller);  
    54.         adapter.notifyDataSetInvalidated();  
    55.         break;  
    56.     case R.id.btn_rotate:  
    57.         animation = new RotateAnimation(0f, 360f);  
    58.         animation.setDuration(500);  
    59.         controller = new LayoutAnimationController(animation, 1f);  
    60.         controller.setOrder(LayoutAnimationController.ORDER_NORMAL);  
    61.         mListView.setLayoutAnimation(controller);  
    62.         adapter.notifyDataSetInvalidated();  
    63.         break;  
    64.     case R.id.btn_scale:  
    65.         animation = new ScaleAnimation(0.1f, 1.0f, 0.1f, 1.0f);  
    66.         animation.setDuration(500);  
    67.         controller = new LayoutAnimationController(animation, 1f);  
    68.         controller.setOrder(LayoutAnimationController.ORDER_NORMAL);  
    69.         mListView.setLayoutAnimation(controller);  
    70.         adapter.notifyDataSetInvalidated();  
    71.         break;  
    72.     case R.id.rotate3d:  
    73.         animation = new Rotate3dAnimation(0, 360, 200, 200, 0, true);  
    74.         animation.setDuration(1000);  
    75.         controller = new LayoutAnimationController(animation, 0.1f);  
    76.         controller.setOrder(LayoutAnimationController.ORDER_NORMAL);  
    77.         mListView.setLayoutAnimation(controller);  
    78.         adapter.notifyDataSetInvalidated();  
    79.         break;  
    80.     default:  
    81.         break;  
    82.     }  
    83.   

Android ListView動畫實現方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.