Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

Android 開發實作:用 Gallery 打造電子相冊

在 iPhone 上用手指拖動圖片滑動,這個互動很加分;Android 當然也能做到,關鍵控件就是 Gallery。本文動手做一個小小的電子相冊。

一、準備圖片資源

先將圖片放進 drawable 目錄,再開一個介面把資源集中管理,日後要增刪照片只改這裡:

public interface ImageResource {
    // 用一個 Integer 陣列保存圖像資源
    Integer[] dImageID = {
        R.drawable.sample_0,
        R.drawable.sample_1,
        R.drawable.sample_2,
        R.drawable.sample_3,
        R.drawable.sample_4,
        R.drawable.sample_5,
        R.drawable.sample_6,
        R.drawable.sample_7,
    };
}

二、版面構想

這個相冊分成上下兩塊:上半部是可拖動的縮圖列表,下半部則把使用者目前選中的圖片放大顯示。上半部由 imageAll 負責,下半部由 imageOne 負責。而 Gallery 要顯示的內容,需要一個繼承自 BaseAdapter 的 ImageAdapter 來提供。

三、完整程式碼

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.BaseAdapter;
import android.widget.ImageView;
import android.widget.Gallery;

public class HelloGallery extends Activity {
    // 用來顯示單張圖像
    private ImageView imageOne;
    // 用來顯示所有圖像
    private ImageView imageAll;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        imageOne = (ImageView) findViewById(R.id.imageView);

        // Gallery 是一個用來水平捲動顯示物件的視圖,這是實現拖動效果的關鍵
        Gallery gallery = (Gallery) findViewById(R.id.gallery);
        // ImageAdapter 就是 Gallery 要顯示的內容
        gallery.setAdapter(new ImageAdapter(this));
    }

    // 實作 ImageResource 介面取得自訂的圖像資源
    class ImageAdapter extends BaseAdapter implements ImageResource {
        // 每個 Gallery 項目的背景資源
        private int galleryItemBackground;
        private Context context;

        public ImageAdapter(Context context) {
            this.context = context;
            // 這裡決定上半部每張圖的背景框,對應 attr.xml
            TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
            galleryItemBackground = a.getResourceId(
                    R.styleable.Gallery1_android_galleryItemBackground, 0);
            a.recycle();
        }

        public int getCount() {
            return dImageID.length;
        }

        public Object getItem(int position) {
            return position;
        }

        // 取得目前呈現在使用者面前的圖像下標
        public long getItemId(int position) {
            // 把該索引的圖像設為 imageOne 顯示
            imageOne.setImageResource(ImageAdapter.dImageID[position]);
            imageOne.setScaleType(ImageView.ScaleType.FIT_CENTER);
            return position;
        }

        // 回傳的 ImageView 就是實現拖動效果的圖像
        public View getView(int position, View convertView, ViewGroup parent) {
            imageAll = new ImageView(context);
            // 設定圖像資源
            imageAll.setImageResource(dImageID[position]);
            // 設定 imageAll 視圖為 120×120
            imageAll.setLayoutParams(new Gallery.LayoutParams(120, 120));
            // FIT_XY 表示填滿 X 與 Y 軸
            imageAll.setScaleType(ImageView.ScaleType.FIT_XY);
            // 設定每個項目的背景資源
            imageAll.setBackgroundResource(galleryItemBackground);
            return imageAll;
        }
    }
}

幾個容易忽略的細節

getItemId() 不只是回傳索引,它同時肩負了「把選中圖片同步到下方大圖」的工作,這是這個小範例把兩層顯示串起來的關鍵。另外 TypedArray 用完記得 recycle(),而 getView() 中的 convertView 在這個範例裡沒有复用,若要優化大量圖片的捲動效能,可以考慮加上 View 复用機制。

Copyright © Linux教程網 All Rights Reserved