Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 介面元件:Gallery 圖庫檢視的實作與原理

編輯:Android 開發實例

Gallery 稱得上是早期 Android 內建的「看圖神器」:它把一堆圖片排成可橫向滑動的膠卷,開發者只要提供一個 Adapter,捲動、對齊與選取的行為都由元件本身處理,成品視覺也相當體面。

實作要點

整個流程只有三件事:在版面配置中放一個 Gallery、寫一個繼承 BaseAdapter 的資料提供者、然後把 Adapter 掛上去。Gallery 自己不存資料,所有內容都靠 Adapter 的 getView() 決定。

版面配置 res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

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

</LinearLayout>

Activity 與自訂 Adapter

MainActivity 只負責取得 Gallery 實例並設定 Adapter;真正的重活在 MyAdapter 裡。

package com.amaker.test;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class MainActivity extends Activity {
    @Override
    // 建立時被呼叫
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Gallery g = (Gallery) findViewById(R.id.Gallery01);
        g.setAdapter(new MyAdapter(this));
    }

    class MyAdapter extends BaseAdapter {
        Context context;
        MyAdapter(Context context) {
            this.context = context;
        }

        private Integer[] imgs = {
            R.drawable.gallery_photo_1, R.drawable.gallery_photo_2,
            R.drawable.gallery_photo_3, R.drawable.gallery_photo_4,
            R.drawable.gallery_photo_5, R.drawable.gallery_photo_6,
            R.drawable.gallery_photo_7, R.drawable.gallery_photo_8,

            R.drawable.gallery_photo_1, R.drawable.gallery_photo_2,
            R.drawable.gallery_photo_3, R.drawable.gallery_photo_4,
            R.drawable.gallery_photo_5, R.drawable.gallery_photo_6,
            R.drawable.gallery_photo_7, R.drawable.gallery_photo_8
        };

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

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

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

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView = new ImageView(context);
            imageView.setImageResource(imgs[position]);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setLayoutParams(new Gallery.LayoutParams(136, 88));
            return imageView;
        }
    }
}

幾個實務提醒

範例刻意把八張圖重複一次,是為了讓滑動效果在兩端看起來更連續,這是常見的展示技巧,正式專案中通常會改成讀取 MediaStore 或網路清單。

getView() 每次都 new 一個 ImageView,在 Gallery 這種需要頻繁重繪的元件上會造成不必要的配置與回收。若要優化,可以沿用 Gallery 提供的 convertView 參數(Gallery 本身沒有像 ListView 那樣廣泛複用,但仍可自行維護一份 View 快取),並把圖片載入交給背景執行緒與快取機制處理,避免大圖造成卡頓。

另外,Gallery 在新版 Android 已被標記為不建議使用,若要在新專案中實作同類效果,通常會改用 RecyclerView 搭配水平的 LayoutManager。

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