編輯:進階開發
ListView 可以說是 Android 開發中使用頻率最高的控制項之一,而為它添加絢麗的特效,幾乎是日後開發中在所難免的課題。這個系列的第一篇,就來教你如何讓列表內容以淡入的方式依序優雅呈現。
這部分想必大家都不陌生,主要分為三個步驟:其一,在版面檔(main.xml)中添加 ListView 控制項;其二,在 layout 中新建一個 ListView 每一項要顯示的內容版面(lvitem.xml);其三,在程式碼中取得 ListView 的引用,為它設定 Adapter 並加入資料。以下直接看程式碼。
main.xml
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="@+id/listv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:layoutAnimation="@anim/animationlayout"
/>
<Button
android:id="@+id/buttonId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="測試"
/>
</LinearLayout>
lvitem.xml
<TableLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/lvitem"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns=" * " >
<TableRow>
<TextView android:id="@+id/tvname"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TextView>
<TextView android:id="@+id/tvage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TextView>
<TextView android:id="@+id/tvsex"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TextView>
</TableRow>
</TableLayout>
DemoActivity.java
package cn.edu.heut.zcl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class DemoActivity extends Activity {
/** Called when the activity is first created. */
ListView lv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv = (ListView) findViewById(R.id.listv);
List<Map<String, String>> data = new ArrayList<Map<String, String>>();
for (int i = 0; i < 10; i++) {
Map<String, String> map = new HashMap<String, String>();
map.put("name", "n" + i);
map.put("age", "age" + i);
map.put("sex", "s" + i);
data.add(map);
}
String[] from = {"name", "age", "sex"};
int[] to = {R.id.tvname, R.id.tvage, R.id.tvsex};
SimpleAdapter sa = new SimpleAdapter(this, data, R.layout.lvitem, from, to);
lv.setAdapter(sa);
}
}
接下來才是本文的重點。ListView 的特效是透過 Animation 來實現的。首先,在 res 中新增一個名為 anim 的資料夾,並在其中建立一個 animationSet 的 XML 檔(animatonset1.xml),加入我們要使用的特效:
<set xmlns:android="https://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:shareInterpolator="true">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="1000" />
</set>
接著要使用 LayoutAnimationController,這裡簡要介紹它的作用:它用於為一個 Layout 裡面的控制項,或是一個 ViewGroup 裡面的控制項設定動畫效果;每一個控制項都會擁有相同的動畫效果;而且可以設定每個控制項動畫的時間差,這些工作既能在 XML 中完成,也能在程式碼中實現。具體的 animationlayout.xml 如下:
<layoutAnimation xmlns:android="https://schemas.android.com/apk/res/android"
android:delay="0.5"
android:animationOrder="random"
android:animation="@anim/animatonset1" />
其中 android:delay 控制每個項目出現的時間間隔,android:animationOrder 則決定了動畫的播放順序(此處設為 random 隨機)。最後,只要在前面寫好的 ListView 的 XML 宣告處,加上 android:layoutAnimation="@anim/animationlayout" 這一行,就能為列表掛上這段動畫效果了。
如此一來,當列表載入時,每一項目便會以淡入的方式依序優雅浮現,為使用者帶來更細膩、更順暢的視覺體驗。
Android 對 Ruby 的支援,將如 Java 一樣自然
JRuby 專案的一位負責人透露,JRuby Builder 將讓以 Ruby 撰寫的應用程式得以運行在 Android 平台上。Engine Yard 的 JRuby
多彩的 Android 手機系統:從 Dalvik 到應用市場的產業鏈效應
Dalvik 虛擬機器依賴 Linux 的一些特性,例如執行緒機制與底層記憶體管理;Linux 核心同時充當硬體與軟體堆疊之間的硬體抽象層。理解這一層關係,大致就掌握了
2010 年最值得擁有的五款 Android 應用
轉眼 2011 年就到了。我們此前介紹過《2010 年 Google 十大 Android 應用》,但那份清單談的是「值得關注」,今天要說的是「值得擁有」。 2010 年
Android 廣播接收器:攔截系統與自訂廣播
廣播接收器(Broadcast Receiver)負責回應來自其他應用程式或系統的廣播訊息,這些訊息也常被稱為事件或意圖。舉例來說,某個 App 可以發出廣播,告知其他