Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 動畫效果(四):LayoutAnimationController

編輯:關於 Android 程式設計

LayoutAnimationController 是什麼

  1. 它用來為一個 layout 裡的控制項,或某個 ViewGroup 裡的子元件設定動畫。
  2. 每個子元件的動畫效果相同。
  3. 但這些元件會在不同的時間點依序出現。
  4. 它既可以在 XML 中設定,也可以在程式碼中建立。

換句話說,它解決的是「整群元件進場的節奏感」問題——最典型的應用就是 ListView 逐列淡入的效果。

在程式碼中使用

  1. 先建立一個 Animation 物件:可以透過 XML 載入,也可以直接用建構子產生。
  2. 用它組出 LayoutAnimationController。
  3. 設定元件的顯示順序。
  4. 把控制器設給 ListView。
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.list_anim);
LayoutAnimationController lac = new LayoutAnimationController(animation);
lac.setOrder(LayoutAnimationController.ORDER_NORMAL);
listView.setLayoutAnimation(lac);

MainActivity.java

public class MainActivity extends ListActivity {

    private Button button = null;
    private ListView listView = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button) findViewById(R.id.buttonId);
        button.setOnClickListener(new buttonListener());

        listView = getListView();
    }

    private ListAdapter buildListAdapter() {
        List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();

        HashMap<String, String> m1 = new HashMap<String, String>();
        m1.put("name", "張三");
        m1.put("gender", "女");

        HashMap<String, String> m2 = new HashMap<String, String>();
        m2.put("name", "李四");
        m2.put("gender", "女");

        HashMap<String, String> m3 = new HashMap<String, String>();
        m3.put("name", "王五");
        m3.put("gender", "男");

        list.add(m1);
        list.add(m2);
        list.add(m3);

        return new SimpleAdapter(MainActivity.this, list, R.layout.item,
                new String[]{"name", "gender"},
                new int[]{R.id.name, R.id.gender});
    }

    private class buttonListener implements OnClickListener {
        @Override
        public void onClick(View v) {
            listView.setAdapter(buildListAdapter());

            // 用程式碼實作,此時不需要 list_anim_layout.xml
            Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.list_anim);
            LayoutAnimationController lac = new LayoutAnimationController(animation);
            lac.setOrder(LayoutAnimationController.ORDER_NORMAL);
            listView.setLayoutAnimation(lac);
        }
    }
}

用 XML 設定(另一種寫法)

list_anim_layout.xml:

<layoutAnimation xmlns:android="https://schemas.android.com/apk/res/android"
    android:delay="0.5"
    android:animationOrder="normal"
    android:animation="@anim/list_anim" />

list_anim.xml(單列本身的動畫):

<set xmlns:android="https://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
    <alpha
        android:fromAlpha="1.0"
        android:toAlpha="0.0"
        android:duration="2000" />
</set>

兩者的分工值得記住:list_anim 描述「每一列怎麼動」,list_anim_layout 描述「整群列怎麼排隊動」。delay 是每列之間的延遲比例,animationOrder 則決定從上往下(normal)還是下往上(reverse)依序出現。

activity_main.xml

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!-- 若改用設定檔方式,可直接在 ListView 上宣告:
    <ListView
        android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        android:layoutAnimation="@anim/list_anim_layout" />
    -->

    <ListView
        android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical" />

    <Button
        android:id="@+id/buttonId"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="測試動畫效果" />
</LinearLayout>

item.xml

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/gender"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved