Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 資訊

Android ListView 進場動畫實作:五種效果一次上手

編輯:Android 資訊

ListView 是 Android 開發裡再熟悉不過的元件,但清一色相同的外觀看久了難免乏味。替列表加上進場動畫,不僅能讓畫面更有層次,也能明顯提升整體的操作體驗。本文整理五種常見的列表動畫效果與其實作方式。


動畫基礎:Tween 與 Rotate3D

前面四種效果都建立在傳統的 Tween 動畫之上,分別是位移(Translate)、透明度(Alpha)、旋轉(Rotate)與縮放(Scale)。第五種則借助自訂的 Rotate3dAnimation 工具類達成 Y 軸 3D 翻轉,建構時需傳入起始角、結束角與旋轉中心座標等參數。

用 LayoutAnimationController 統一調度

LayoutAnimationController 能讓一組子元件依序出現,搭配 ListView 的 setLayoutAnimation() 即可讓整份清單逐項進場。核心程式碼如下:

private Button button, button2, button3, button4, button5;
private ListView mListView;
private Animation animation;
private LayoutAnimationController controller;
private String[] arry = { "一", "二", "三", "四", "五", "六" };
private ArrayAdapter<String> adapter;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arry);
    mListView.setAdapter(adapter);
}

private void initView() {
    mListView = (ListView) findViewById(R.id.list);
    button  = (Button) findViewById(R.id.btn_tran);
    button2 = (Button) findViewById(R.id.btn_alpha);
    button3 = (Button) findViewById(R.id.btn_rotate);
    button4 = (Button) findViewById(R.id.btn_scale);
    button5 = (Button) findViewById(R.id.rotate3d);
    button.setOnClickListener(this);
    button2.setOnClickListener(this);
    button3.setOnClickListener(this);
    button4.setOnClickListener(this);
    button5.setOnClickListener(this);
}

@Override
public void onClick(View arg0) {
    // ORDER_NORMAL 順序、ORDER_REVERSE 反向、ORDER_RANDOM 隨機
    switch (arg0.getId()) {
        case R.id.btn_tran:
            animation = new TranslateAnimation(-50f, 0f, 0f, 0f);
            animation.setDuration(500);
            controller = new LayoutAnimationController(animation, 1f); // 第二個參數為項間延遲
            controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
            mListView.setLayoutAnimation(controller);
            adapter.notifyDataSetInvalidated();
            break;
        case R.id.btn_alpha:
            animation = new AlphaAnimation(0f, 1f);
            animation.setDuration(500);
            controller = new LayoutAnimationController(animation, 1f);
            controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
            mListView.setLayoutAnimation(controller);
            adapter.notifyDataSetInvalidated();
            break;
        case R.id.btn_rotate:
            animation = new RotateAnimation(0f, 360f);
            animation.setDuration(500);
            controller = new LayoutAnimationController(animation, 1f);
            controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
            mListView.setLayoutAnimation(controller);
            adapter.notifyDataSetInvalidated();
            break;
        case R.id.btn_scale:
            animation = new ScaleAnimation(0.1f, 1.0f, 0.1f, 1.0f);
            animation.setDuration(500);
            controller = new LayoutAnimationController(animation, 1f);
            controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
            mListView.setLayoutAnimation(controller);
            adapter.notifyDataSetInvalidated();
            break;
        case R.id.rotate3d:
            animation = new Rotate3dAnimation(0, 360, 200, 200, 0, true);
            animation.setDuration(1000);
            controller = new LayoutAnimationController(animation, 0.1f);
            controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
            mListView.setLayoutAnimation(controller);
            adapter.notifyDataSetInvalidated();
            break;
    }
}

掌握這套寫法後,只要替換 Animation 實作並調整 duration 與 delay,就能自由變化出符合產品調性的進場效果。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved