2014/08/05

Android Gallery

最近在當Android助教,所以紀錄一下

XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.ch.mygallery.MainActivity" >

    <Gallery
        android:id="@+id/Gallery"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true" />

    <ImageView
        android:id="@+id/ImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="145dp" />

</RelativeLayout>




Code:
package com.cy.mygallery;

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

public class MainActivity extends Activity {
 private ImageView imageView;

 // 圖片ID
 protected final static int ImageID[] = { R.drawable.a1, R.drawable.a2,
   R.drawable.a3, R.drawable.a4, R.drawable.a5 };

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  Gallery gallery = (Gallery) findViewById(R.id.Gallery);
  imageView = (ImageView) findViewById(R.id.ImageView);
  MyAdapter adapter = new MyAdapter(this);

  // 設定圖片之間的間距
  gallery.setSpacing(3);

  // 設定橋接器
  gallery.setAdapter(adapter);

  // 設定畫廊的選擇觸發事件
  gallery.setOnItemSelectedListener(onItemSelectedListener);

 }

 // 畫廊選擇觸發事件
 private AdapterView.OnItemSelectedListener onItemSelectedListener = new OnItemSelectedListener() {

  @Override
  public void onItemSelected(AdapterView<?> adapterView, View view,
    int postion, long arg3) {

   // 將選取圖片指定給ImageView
   imageView.setImageResource(MainActivity.ImageID[postion]);

  }

  @Override
  public void onNothingSelected(AdapterView<?> arg0) {
  }
 };

}

class MyAdapter extends BaseAdapter {
 private Context context;

 public MyAdapter(Context context) {
  this.context = context;
 }

 @Override
 public int getCount() {
  // 取得影像數量
  return MainActivity.ImageID.length;
 }

 @Override
 public Object getItem(int postion) {
  // 取得位置
  return postion;
 }

 @Override
 public long getItemId(int id) {
  // 取得編號
  return id;
 }

 @Override
 public View getView(int postion, View view, ViewGroup viewGroup) {
  ImageView imageView = new ImageView(context);

  // 設定影像來源
  imageView.setImageResource(MainActivity.ImageID[postion]);

  // 將圖片置中
  imageView.setScaleType(ScaleType.FIT_CENTER);

  // 設定圖示大小
  imageView.setLayoutParams(new Gallery.LayoutParams(90, 90));
  return imageView;
 }
}

執行結果: