編輯:關於 Android 程式設計
換句話說,它解決的是「整群元件進場的節奏感」問題——最典型的應用就是 ListView 逐列淡入的效果。
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.list_anim); LayoutAnimationController lac = new LayoutAnimationController(animation); lac.setOrder(LayoutAnimationController.ORDER_NORMAL); listView.setLayoutAnimation(lac);
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);
}
}
}
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)依序出現。
<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>
<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>
數據結構之線索二叉樹的前序、中序與後序遍歷
線索化二叉樹概述 二叉樹是一種非線性結構。在之前實現的二叉樹遍歷中,無論遞歸還是非遞歸,用二叉樹作為存儲結構時只能取到該結點的左孩子和右孩子,無法得到該結點的前驅和後繼。
ToggleButton 開關按鈕控件的使用方法
ToggleButton 是一個帶有開/關狀態切換的按鈕控件,本文整理它的基本用法。 一、簡介 從類結構來看,ToggleButton 的父類是 CompoundBut
詳解Android TabHost組件的布局要點
TabHost 是早期 Android 實作分頁切換的標準做法。它的結構有幾個「id 必須寫死」的硬性規定,很多人卡關其實都栽在這裡。本文先把三個必要元素的規則講清楚,再
Android 中搭建 FTP 伺服器的入門指南
先簡單介紹一下 FTP:FTP(File Transfer Protocol)即檔案傳輸協定。它的作用是讓使用者連上一台運行著 FTP 伺服器程式的遠端電腦,瀏覽遠端有哪