1. 程式人生 > >使用Gallery控制元件實現個人相簿功能

使用Gallery控制元件實現個人相簿功能

首先將圖片匯入“Drawable”目錄,在響應onCreate的同時將素材圖片載入到Gallery Widget中;然後新增一個OnItemClick事件以取得圖片的ID編號,這樣就可以響應使用者單擊圖片的狀態,完成Gallery的高階使用。

在main.xml檔案中插入一個Gallery控制元件:

<?xml version="1.0" encoding="utf-8"?>
<Gallery xmlns:android="http://schemas.android.com/apk/res/android" 
  android:id="@+id/mygallery"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
/>

編寫attrs.xml檔案,設定定義Layout外部資源的樣式,並且設定隨著滑動而改變Layout背景圖片效果。

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <declare-styleable name="Gallery">
    <attr name="android:galleryItemBackground" />
  </declare-styleable> 
</resources>
ImageAdapter繼承於BaseAdapter類的未實現方法的重寫構造,通過Gallery中的OnItemClick()方法來響應圖片滑動及Layout的寬和高的設定。
package com.EX027;

import com.EX027.R;

import android.app.Activity;
import android.os.Bundle;

/* 本範例需使用到的class */
import android.content.Context;
import android.content.res.TypedArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class EX027 extends Activity 
{
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) 
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    /*通過findViewById取得*/
    Gallery g = (Gallery) findViewById(R.id.mygallery);
    /* 新增一ImageAdapter並設定給Gallery物件 */
    g.setAdapter(new ImageAdapter(this));
    
    /* 設定一個itemclickListener並Toast被點選圖片的位置 */
    g.setOnItemClickListener(new OnItemClickListener() 
    {
      public void onItemClick
      (AdapterView<?> parent, View v, int position, long id)
      {
        Toast.makeText
        (EX027.this, getString(R.string.my_gallery_text_pre)
        + position+ getString(R.string.my_gallery_text_post), 
        Toast.LENGTH_SHORT).show();
      }
    });
  }
  
  /* 改寫BaseAdapter自定義一ImageAdapter class */
  public class ImageAdapter extends BaseAdapter 
  {
    /*宣告變數*/
    int mGalleryItemBackground;
    private Context mContext;
    
    /*ImageAdapter的構造器*/
    public ImageAdapter(Context c) 
    {
      mContext = c;
      
      /* 使用在res/values/attrs.xml中的<declare-styleable>定義
      * 的Gallery屬性.*/
      TypedArray a = obtainStyledAttributes(R.styleable.Gallery);
      
      /*取得Gallery屬性的Index id*/
      mGalleryItemBackground = a.getResourceId
      (R.styleable.Gallery_android_galleryItemBackground, 0);
      
      /*讓物件的styleable屬效能夠反覆使用*/ 
      a.recycle();
    }
    
    /* 覆蓋的方法getCount,返回圖片數目 */
    public int getCount() 
    {
      return myImageIds.length;
    }
         
    /* 覆蓋的方法getItemId,返回影象的陣列id */

    public Object getItem(int position) 
    {
      return position;
    }
    public long getItemId(int position) 
    {
      return position;
    }
    
    /* 覆蓋的方法getView,返回一View物件 */
    public View getView
    (int position, View convertView, ViewGroup parent)
    {
      /*產生ImageView物件*/
      ImageView i = new ImageView(mContext);
      /*設定圖片給imageView物件*/
      i.setImageResource(myImageIds[position]);
      /*重新設定圖片的寬高*/
      i.setScaleType(ImageView.ScaleType.FIT_XY);
      /*重新設定Layout的寬高*/
      i.setLayoutParams(new Gallery.LayoutParams(136, 88));
      /*設定Gallery背景圖*/
      i.setBackgroundResource(mGalleryItemBackground);
      /*返回imageView物件*/
      return i;
    }
    
    /*建構一Integer array並取得預載入Drawable的圖片id*/
    private Integer[] myImageIds = 
    {
      R.drawable.photo1,
      R.drawable.photo2,
      R.drawable.photo3,
      R.drawable.photo4,
      R.drawable.photo5,
      R.drawable.photo6,
    };   
  } 
}