Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 圖像顯示(十四):Gallery 與 ImageView 的搭配

編輯:關於 Android 程式設計

想在一個畫面裡展示一組可以左右滑動的圖片,並在點選後於另一處放大顯示?Gallery 加上 ImageView 就是最經典的組合。Gallery 負責橫向捲動的縮圖列表,ImageView 則用來呈現使用者選中的那張圖。

版面配置

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Images of San Francisco" />

    <Gallery
        android:id="@+id/gallery1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/image1"
        android:layout_width="320dp"
        android:layout_height="250dp"
        android:scaleType="fitXY" />
</LinearLayout>

別忘了在 res/values 下建立 attrs.xml,宣告 Gallery 的樣式屬性:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="Gallery1">
        <attr name="android:galleryItemBackground" />
    </declare-styleable>
</resources>

用 BaseAdapter 把 Gallery 與圖片綁定

Gallery 要顯示什麼,取決於它的 Adapter。我們自訂一個繼承 BaseAdapter 的 ImageAdapter,充當 Gallery 與圖片資源之間的橋梁。事實上,ListView、GridView、Spinner、Gallery 都使用 BaseAdapter 這類的結構;而它也有 ArrayAdapter、CursorAdapter、SpinnerAdapter 等常見子類。

public class GalleryActivity extends Activity {

    Integer[] imageIDs = {
        R.drawable.pic1, R.drawable.pic2, R.drawable.pic3,
        R.drawable.pic4, R.drawable.pic5, R.drawable.pic6, R.drawable.pic7
    };

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

        Gallery gallery = (Gallery) findViewById(R.id.gallery1);
        gallery.setAdapter(new ImageAdapter(this));
        gallery.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {
                Toast.makeText(getBaseContext(),
                        "pic" + (position + 1) + " selected",
                        Toast.LENGTH_SHORT).show();

                // 在 ImageView 中顯示被選中的圖片
                ImageView imageView = (ImageView) findViewById(R.id.image1);
                imageView.setImageResource(imageIDs[position]);
            }
        });
    }
}

ImageAdapter 的實作重點

Adapter 主要需實作四個方法:getCount() 回傳圖片數量、getItem() 與 getItemId() 回傳項目資訊,以及最關鍵的 getView()——它負責產生每一個縮圖 view。這裡有個效能小技巧:先檢查 convertView 是否為 null,不為 null 就重複使用,避免頻繁配置物件。

public class ImageAdapter extends BaseAdapter {
    Context context;
    int itemBackground;

    public ImageAdapter(Context c) {
        context = c;
        TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
        itemBackground = a.getResourceId(
                R.styleable.Gallery1_android_galleryItemBackground, 0);
        a.recycle();
    }

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

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

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            imageView = new ImageView(context);
            imageView.setImageResource(imageIDs[position]);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));
        } else {
            imageView = (ImageView) convertView;
        }
        imageView.setBackgroundResource(itemBackground);
        return imageView;
    }
}

完成後按 F11 執行,你會看到一排可以左右滑動的縮圖;點擊任一張,下方的 ImageView 就會顯示對應的大圖並跳出提示訊息。這套「Gallery 選圖 + ImageView 呈現」的模式,是許多圖片瀏覽功能的基礎原型。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved