我們在智能手機上想必都見過那種可以左右滑動的圖片集,這種效果在Android裡正是靠Gallery來實現的。Gallery是一種內部子元素能夠水平滾動、並會自動把當前選中項居中定位的布局組件。本篇就來動手做一個Gallery畫廊範例。
先上一張運行效果圖,讓大家有個直觀的認識。

下面直接上代碼,關鍵解釋都放在註釋裡。
第一步,新建項目HelloGallery。
第二步,把wallpaper_0.jpg到wallpaper_9.jpg共10張圖片拷貝到res/drawable目錄。
第三步,編寫res/layout/main.xml。這裡用FrameLayout實現圖層疊加:底層的ImageView顯示大圖,上層的Gallery展示可滑動的縮略畫廊。
- <?xml version="1.0" encoding="utf-8"?>
- <framelayout android:layout_height="fill_parent" android:layout_width="fill_parent" xmlns:android="https://schemas.android.com/apk/res/android" android:id="@+id/FrameLayout01">
- <imageview android:layout_height="fill_parent" android:layout_width="fill_parent" android:id="@+id/ImageView01" android:src="@drawable/wallpaper_0"></imageview>
- <gallery android:layout_height="wrap_content" android:layout_width="fill_parent" xmlns:android="https://schemas.android.com/apk/res/android" android:id="@+id/Gallery01" android:spacing="5dp"></gallery>
- </framelayout>
其中android:spacing="5dp"用來設定縮略圖之間的間距。
第四步,在res/values/目錄新建attrs.xml,聲明一個可樣式化屬性,供Gallery子項背景使用。
- <?xml version="1.0" encoding="UTF-8"?>
- <resources>
- <declare-styleable name="HelloGallery">
- <attr name="android:galleryItemBackground"></attr>
- </declare-styleable>
- </resources>
第五步,編寫MainHelloGallery.java。核心在於自定義一個繼承BaseAdapter的ImageAdapter,負責生成每個縮略圖視圖;同時為Gallery設置OnItemClickListener,點擊後把選中圖片顯示到背景的大ImageView上。
- package android.basic.lesson13;
- import android.app.Activity;
- import android.content.Context;
- import android.content.res.TypedArray;
- import android.os.Bundle;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.BaseAdapter;
- import android.widget.Gallery;
- import android.widget.ImageView;
- import android.widget.Toast;
- public class MainHelloGallery extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- final ImageView iv=(ImageView)findViewById(R.id.ImageView01);
- Gallery g=(Gallery)findViewById(R.id.Gallery01);
- //為Gallery設置圖片適配器
- g.setAdapter(new ImageAdapter(this));
- //Gallery是AdapterView子類,設置項點擊監聽
- g.setOnItemClickListener(new OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- //提示點擊的是第幾張
- Toast.makeText(MainHelloGallery.this, "" + position, Toast.LENGTH_LONG).show();
- //把選中項顯示到背景大圖
- iv.setImageResource(((ImageView)view).getId());
- }
- });
- }
- //自定義繼承BaseAdapter的圖片適配器
- public class ImageAdapter extends BaseAdapter {
- int mGalleryItemBackground;
- private Context mContext;
- private Integer[] mImageIds = { R.drawable.wallpaper_0, R.drawable.wallpaper_1, R.drawable.wallpaper_2, R.drawable.wallpaper_3, R.drawable.wallpaper_4, R.drawable.wallpaper_5, R.drawable.wallpaper_6, R.drawable.wallpaper_7, R.drawable.wallpaper_8, R.drawable.wallpaper_9 };
- public ImageAdapter(Context c){
- mContext = c;
- //讀取styleable資源
- TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);
- mGalleryItemBackground = a.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground, 0);
- a.recycle();
- }
- @Override
- public int getCount() {
- return mImageIds.length;
- }
- @Override
- public Object getItem(int position) {
- return position;
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- ImageView iv = new ImageView(mContext);
- iv.setImageResource(mImageIds[position]);
- //給ImageView設置Id,否則默認都是-1
- iv.setId(mImageIds[position]);
- iv.setLayoutParams(new Gallery.LayoutParams(120, 160));
- iv.setScaleType(ImageView.ScaleType.FIT_XY);
- iv.setBackgroundResource(mGalleryItemBackground);
- return iv;
- }
- }
- }
點擊某張縮略圖時,該子元素的圖片就會顯示到背後那層ImageView上。感興趣的同學可以再了解一下AdapterView的繼承體系:

掌握了Gallery的用法,你就能輕鬆做出炫酷的圖片滑動效果了。