編輯:關於 Android 程式設計
GridView 與 ImageSwitcher 是一組很自然的搭配:前者負責以網格形式排列縮圖,後者負責以大圖形式呈現並提供切換動畫。這篇文章透過一個完整範例把兩者串起來。
GridView 用於在介面上按列、行分佈顯示多個元件。它與 ListView 有共同父類 AbsListView,差別在於分佈方向:ListView 只在單一方向上排列,GridView 則同時橫向與縱向排列。
這也解釋了一個實務細節:使用 GridView 時通常要明確指定 numColumns 大於 1,因為該屬性的預設值為 1——一旦只有一欄,GridView 就退化成 ListView 了。

ImageSwitcher 由 FrameLayout 派生而來,與 ImageView 相似,但多了一項關鍵能力:圖片切換時可以套用動畫效果。
使用時必須先設定一個 ViewFactory,由它的 makeView() 負責產生實際顯示用的 ImageView。這是因為 ImageSwitcher 內部需要預先準備好進出兩張圖以完成切換動畫,不能直接塞圖片進去。
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal">
<GridView
android:id="@+id/grid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:horizontalSpacing="1dp"
android:verticalSpacing="2dp"
android:numColumns="4" />
<ImageSwitcher
android:id="@+id/imageSwitch"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
public class MainActivity extends Activity {
private GridView gredView = null;
private ImageSwitcher imageSwt = null;
// 圖片資源
final int images[] = new int[] {
R.drawable.name01, R.drawable.name02, R.drawable.name03, R.drawable.name04,
R.drawable.name05, R.drawable.name06, R.drawable.name07, R.drawable.name08,
R.drawable.name09, R.drawable.name10, R.drawable.name11, R.drawable.name12,
R.drawable.name13, R.drawable.name14, R.drawable.name15, R.drawable.name16
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gredView = (GridView) findViewById(R.id.grid);
imageSwt = (ImageSwitcher) findViewById(R.id.imageSwitch);
// 組裝 SimpleAdapter 所需的資料
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (int i = 0; i < images.length; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("img", images[i]);
list.add(map);
}
// 淡入
imageSwt.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
// 淡出
imageSwt.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
// 設定 ViewFactory,負責產生承載圖片的 ImageView
imageSwt.setFactory(new ViewFactory() {
@Override
public View makeView() {
ImageView imageView = new ImageView(MainActivity.this);
imageView.setLayoutParams(
new ImageSwitcher.LayoutParams(300, 300));
imageView.setScaleType(ScaleType.FIT_CENTER);
return imageView;
}
});
SimpleAdapter simpleAdapter = new SimpleAdapter(this, list,
R.layout.img, new String[] { "img" }, new int[] { R.id.img });
gredView.setAdapter(simpleAdapter);
// 選中監聽(焦點移動時觸發)
gredView.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
imageSwt.setImageResource(images[position]);
}
@Override
public void onNothingSelected(AdapterView<?> parent) { }
});
// 點擊監聽
gredView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
imageSwt.setImageResource(images[position]);
}
});
}
}
執行後,下方網格展示全部縮圖,上方 ImageSwitcher 以淡入淡出效果顯示目前選中的大圖。

onItemSelected 與 onItemClick 的差別。前者在焦點移動時觸發(含方向鍵、觸控移動),後者只在點擊時觸發。範例中兩者都綁定同一個動作,是為了讓「滑過」與「點擊」都能預覽;若只想點擊才切換,留 onItemClick 即可。
動畫可用系統預設。android.R.anim 內建了 fade_in、fade_out、slide_in_left、slide_out_right 等,不必自己寫 XML。想要更花俏的效果,可在 res/anim 下自定義 set。
大圖建議改用 Gallery 之外的方案。Gallery 在 API 16 後已棄用。若要做全螢幕大圖瀏覽,現在更合適的選擇是 ViewPager 加 PhotoView,或直接整合 PhotoView 的 Attacher 來支援雙擊縮放與手勢。
注意記憶體。範例直接從 drawable 載入 16 張圖,圖片一大就會 OOM。正式專案應配合 BitmapFactory 的 inSampleSize 降採樣,並搭配 LruCache 做快取。
Android Binder 與 AIDL:跨進程通信機制與實作
前言Android 的 Binder 機制是跨進程通信的核心。AIDL 則是基於 Binder 的介面描述語言,可簡化 IPC 開發。本文說明 Binder 概念、AID
Android Service 與進程間通信(AIDL/IPC)完整解析
Service 是什麼 Service 是在後台長期運作、沒有使用者介面的元件。其他元件可以啟動它讓它在後台跑,也可以綁定它進行互動,甚至藉此實現跨進程通信(IPC)。
IntentService 全面解析:使用方式、執行流程與源碼剖析
一、IntentService 是什麼 理解 IntentService,可以從三個問題入手:它是什麼?怎麼用?內部如何運作? 官方文檔的說明如下: // 是什麼、怎麼
Android 基本控件 ToggleButton 與 Switch 使用指南
ToggleButton(開關按鈕)與 Switch(開關)講解: 一、核心屬性講解: (1)ToggleButton textOn:按鈕被選中時顯示的文字 textOf