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

Android 入門第十二篇:Gallery 看圖元件入門

編輯:Android 開發實例

Gallery 是 Android 相當好用的看圖元件, horizontal 捲動的縮圖列加上中間放大的選中效果,天生就適合做相簿。它替開發者省下不少自幹捲動與對齊的工夫,視覺呈現也頗為體面。本文要示範 Gallery 的基本用法,並用反射機制動態讀取資源中的圖片,省去逐一列 ID 的麻煩。

main.xml 源碼:

 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.     > 
  7. <Gallery android:id="@+id/gallery" android:layout_height="fill_parent" android:layout_width="fill_parent"></Gallery> 
  8. </LinearLayout> 

Adapter 才是主角

Gallery 本身只是個容器,真正決定顯示什麼、怎麼顯示的是 Adapter。這裡自訂 ImageAdapter 繼承 BaseAdapter,在建構子中透過 R.drawable.class.getDeclaredFields() 把所有資源欄位掃過一遍,跳過 icon 之後,用 field.getInt 取出每張圖的 ID 存進 imgList;同時順手用 BitmapFactory 解開圖片、量出寬高,一併存到 imgSizes,供稍後設定 Layout 尺寸使用。

getView 是每次要顯示縮圖時被呼叫的地方。這裡直接 new 一個 ImageView,用 setImageResource 塞入對應圖片,setScaleType(FIT_XY) 讓圖片填滿邊界,最後以 Gallery.LayoutParams 套用前面量到的實際寬高。Gallery 的子項必須使用 Gallery.LayoutParams,用一般的 LinearLayout 參數是不會生效的。

最後掛上 OnItemClickListener,點一下就把目前 position 寫進標題,方便確認事件有沒有觸發。

程式源碼:

 
  1. package com.testImageView;     
  2.     
  3. import java.lang.reflect.Field;     
  4. import java.util.ArrayList;     
  5.     
  6. import android.app.Activity;     
  7. import android.content.Context;     
  8. import android.graphics.Bitmap;     
  9. import android.graphics.BitmapFactory;     
  10. import android.os.Bundle;     
  11. import android.view.View;     
  12. import android.view.ViewGroup;     
  13. import android.widget.AdapterView;     
  14. import android.widget.BaseAdapter;     
  15. import android.widget.Gallery;     
  16. import android.widget.ImageView;     
  17. import android.widget.AdapterView.OnItemClickListener;     
  18.     
  19. public class testImageView extends Activity {     
  20.     private Gallery mGallery;     
  21.     @Override    
  22.     public void onCreate(Bundle savedInstanceState) {     
  23.         super.onCreate(savedInstanceState);     
  24.         setContentView(R.layout.main);     
  25.              
  26.         mGallery = (Gallery)findViewById(R.id.gallery);     
  27.         try {     
  28.             mGallery.setAdapter(new ImageAdapter(this));     
  29.         } catch (IllegalArgumentException e) {     
  30.             // TODO Auto-generated catch block     
  31.             e.printStackTrace();     
  32.         } catch (IllegalAccessException e) {     
  33.             e.printStackTrace();     
  34.         }     
  35.         mGallery.setOnItemClickListener(new OnItemClickListener() {     
  36.             public void onItemClick(AdapterView parent, View v, int position, long id) {     
  37.                 testImageView.this.setTitle(String.valueOf(position));     
  38.             }     
  39.         });     
  40.     }     
  41.       
  42.     /*    
  43.     * class ImageAdapter is used to control gallery source and operation.    
  44.     */    
  45.     private class ImageAdapter extends BaseAdapter{     
  46.         private Context mContext;     
  47.         private ArrayList<Integer> imgList=new ArrayList<Integer>();     
  48.         private ArrayList<Object> imgSizes=new ArrayList<Object>();     
  49.         public ImageAdapter(Context c) throws IllegalArgumentException, IllegalAccessException{     
  50.             mContext = c;     
  51.                  
  52.             //用反射機制來獲取資源中的圖片ID和尺寸     
  53.             Field[] fields = R.drawable.class.getDeclaredFields();     
  54.             for (Field field : fields)     
  55.             {     
  56.                 if (!"icon".equals(field.getName()))//除了icon之外的圖片     
  57.                 {        
  58.                     int index=field.getInt(R.drawable.class);     
  59.                     //保存圖片ID     
  60.                     imgList.add(index);     
  61.                     //保存圖片大小     
  62.                     int size[]=new int[2];     
  63.                     Bitmap bmImg=BitmapFactory.decodeResource(getResources(),index);     
  64.                     size[0]=bmImg.getWidth();size[1]=bmImg.getHeight();     
  65.                     imgSizes.add(size);     
  66.                 }     
  67.            }     
  68.         }     
  69.         @Override    
  70.         public int getCount() {     
  71.             // TODO Auto-generated method stub     
  72.     
  73.             return imgList.size();     
  74.         }     
  75.     
  76.         @Override    
  77.         public Object getItem(int position) {     
  78.             // TODO Auto-generated method stub     
  79.     
  80.             return position;     
  81.         }     
  82.     
  83.         @Override    
  84.         public long getItemId(int position) {     
  85.             // TODO Auto-generated method stub     
  86.     
  87.             return position;     
  88.         }     
  89.     
  90.         @Override    
  91.         public View getView(int position, View convertView, ViewGroup parent) {     
  92.             // TODO Auto-generated method stub     
  93.     
  94.             ImageView i = new ImageView (mContext);     
  95.             //從imgList取得圖片ID     
  96.             i.setImageResource(imgList.get(position).intValue());     
  97.             i.setScaleType(ImageView.ScaleType.FIT_XY);     
  98.             //從imgSizes取得圖片大小     
  99.             int size[]= new int[2];     
  100.             size=(int[]) imgSizes.get(position);     
  101.             i.setLayoutParams(new Gallery.LayoutParams(size[0], size[1]));     
  102.             return i;     
  103.         }     
  104.              
  105.     };     
  106. } 
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved