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

Android Gallery 控制項用法解析

編輯:關於 Android 程式設計

Gallery 基本概念

Gallery 主要用於橫向顯示圖片列表。若要讓圖片循環顯示,可透過自訂 Adapter 並搭配取餘運算來實現。

版面配置

<Gallery android:id="@+id/gallery"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:spacing="10dip"
    android:unselectedAlpha="1.2" />

<ImageSwitcher android:id="@+id/imageSwitcher"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

自訂 Adapter

public int getCount() {
    return Integer.MAX_VALUE;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ImageView iv = new ImageView(context);
    iv.setImageResource(imageIDs[position % imageIDs.length]);
    iv.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    iv.setLayoutParams(new LinearLayout.LayoutParams(77, 77));
    return iv;
}

搭配 ImageSwitcher

當使用者選擇 Gallery 中的圖片時,可透過 OnItemSelectedListener 更新 ImageSwitcher,顯示放大後的圖片。

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