androdi 的 gallery

Android的Gallery控件是个很不错的看图控件,大大减轻了开发者对于看图功能的开发,而且效果也比较美观。本文介绍Gallery的用法,用反射机制来动态读取资源中的图片。

 

main.xml源码:

 

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

 

 

程序源码:

 

package com.testImageView;   
  1.   
  2. import java.lang.reflect.Field;   
  3. import java.util.ArrayList;   
  4.   
  5. import android.app.Activity;   
  6. import android.content.Context;   
  7. import android.graphics.Bitmap;   
  8. import android.graphics.BitmapFactory;   
  9. import android.os.Bundle;   
  10. import android.view.View;   
  11. import android.view.ViewGroup;   
  12. import android.widget.AdapterView;   
  13. import android.widget.BaseAdapter;   
  14. import android.widget.Gallery;   
  15. import android.widget.ImageView;   
  16. import android.widget.AdapterView.OnItemClickListener;   
  17.   
  18. public class testImageView extends Activity {   
  19.     private Gallery mGallery;   
  20.     @Override  
  21.     public void onCreate(Bundle savedInstanceState) {   
  22.         super.onCreate(savedInstanceState);   
  23.         setContentView(R.layout.main);   
  24.            
  25.         mGallery = (Gallery)findViewById(R.id.gallery);   
  26.         try {   
  27.             mGallery.setAdapter(new ImageAdapter(this));   
  28.         } catch (IllegalArgumentException e) {   
  29.             // TODO Auto-generated catch block   
  30.             e.printStackTrace();   
  31.         } catch (IllegalAccessException e) {   
  32.             // TODO Auto-generated catch block   
  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. }  

posted on 2012-03-30 10:47  lizhen-cswb  阅读(279)  评论(0编辑  收藏  举报

导航