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

Android 入門:Gallery 控件的基礎用法實例

編輯:關於 Android 程式設計

Gallery 是 Android 裡相當好用的看图控件,能大幅減少開發看图功能的工作量,而且效果本身就頗為美觀。本文的實例有一個重點:用反射機制動態讀取資源中的圖片,而不是把每張圖的 ID 硬寫在程式碼裡。

程式執行效果:

 

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/gallery" android:layout_height="fill_parent" android:layout_width="fill_parent"></Gallery> </LinearLayout> 

Java 程式碼:

 package com.testImageView; import java.lang.reflect.Field; import java.util.ArrayList; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; 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.AdapterView.OnItemClickListener; public class testImageView extends Activity { private Gallery mGallery; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mGallery = (Gallery)findViewById(R.id.gallery); try { mGallery.setAdapter(new ImageAdapter(this)); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } mGallery.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView parent, View v, int position, long id) { testImageView.this.setTitle(String.valueOf(position)); } }); } /* * ImageAdapter 負責控制Gallery的數據源與操作 */ private class ImageAdapter extends BaseAdapter{ private Context mContext; private ArrayList<Integer> imgList=new ArrayList<Integer>(); private ArrayList<Object> imgSizes=new ArrayList<Object>(); public ImageAdapter(Context c) throws IllegalArgumentException, IllegalAccessException{ mContext = c; //用反射機制來獲取資源中的圖片ID和尺寸 Field[] fields = R.drawable.class.getDeclaredFields(); for (Field field : fields) { if (!"icon".equals(field.getName()))//除了icon之外的圖片 { int index=field.getInt(R.drawable.class); imgList.add(index);//保存圖片ID int size[]=new int[2]; Bitmap bmImg=BitmapFactory.decodeResource(getResources(),index); size[0]=bmImg.getWidth();size[1]=bmImg.getHeight(); imgSizes.add(size);//保存圖片大小 } } } @Override public int getCount() { return imgList.size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ImageView i = new ImageView (mContext); i.setImageResource(imgList.get(position).intValue());//從imgList取得圖片ID i.setScaleType(ImageView.ScaleType.FIT_XY); int size[]= new int[2]; size=(int[]) imgSizes.get(position);//從imgSizes取得圖片大小 i.setLayoutParams(new Gallery.LayoutParams(size[0], size[1])); return i; } }; }

反射這一招看起來只是少寫幾行,實際上在圖片數量會變動的專案裡非常實用:往 drawable 目錄丟圖就能自動出現,不必回頭改 Adapter。要注意的是這裡把「排除 icon」寫死了,正式專案中通常會改用命名規範或資源 ID 陣列來過濾,避免誤抓。

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