Android入門教程(二十三)——之Gallery)

來源:互聯網
上載者:User

 本文來自http://blog.csdn.net/hellogv/ ,引用必須註明出處!

       Android的Gallery控制項是個很不錯的看圖控制項,大大減輕了開發人員對於看圖功能的開發,而且效果也比較美觀。本文介紹Gallery的用法,用反射機制來動態讀取資源中的圖片。

       本文的:

 

main.xml源碼:

view plain copy to clipboard print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://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>  

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

 

 

程式源碼:

view plain copy to clipboard print ?
  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.             // TODO Auto-generated catch block   
  34.             e.printStackTrace();  
  35.         }  
  36.         mGallery.setOnItemClickListener(new OnItemClickListener() {  
  37.             public void onItemClick(AdapterView parent, View v, int position, long id) {  
  38.                 testImageView.this.setTitle(String.valueOf(position));  
  39.             }  
  40.         });  
  41.     }  
  42.       
  43.     /* 
  44.      * class ImageAdapter is used to control gallery source and operation. 
  45.      */  
  46.     private class ImageAdapter extends BaseAdapter{  
  47.         private Context mContext;  
  48.         private ArrayList<Integer> imgList=new ArrayList<Integer>();  
  49.         private ArrayList<Object> imgSizes=new ArrayList<Object>();  
  50.         public ImageAdapter(Context c) throws IllegalArgumentException, IllegalAccessException{  
  51.             mContext = c;  
  52.               
  53.             //用反射機制來擷取資源中的圖片ID和尺寸   
  54.             Field[] fields = R.drawable.class.getDeclaredFields();  
  55.             for (Field field : fields)  
  56.             {  
  57.                 if (!"icon".equals(field.getName()))//除了icon之外的圖片   
  58.                 {     
  59.                     int index=field.getInt(R.drawable.class);  
  60.                     //儲存圖片ID   
  61.                     imgList.add(index);  
  62.                     //儲存圖片大小   
  63.                     int size[]=new int[2];  
  64.                     Bitmap bmImg=BitmapFactory.decodeResource(getResources(),index);  
  65.                     size[0]=bmImg.getWidth();size[1]=bmImg.getHeight();  
  66.                     imgSizes.add(size);  
  67.                 }  
  68.             }  
  69.         }  
  70.         @Override  
  71.         public int getCount() {  
  72.             // TODO Auto-generated method stub   
  73.   
  74.             return imgList.size();  
  75.         }  
  76.   
  77.         @Override  
  78.         public Object getItem(int position) {  
  79.             // TODO Auto-generated method stub   
  80.   
  81.             return position;  
  82.         }  
  83.   
  84.         @Override  
  85.         public long getItemId(int position) {  
  86.             // TODO Auto-generated method stub   
  87.   
  88.             return position;  
  89.         }  
  90.   
  91.         @Override  
  92.         public View getView(int position, View convertView, ViewGroup parent) {  
  93.             // TODO Auto-generated method stub   
  94.   
  95.             ImageView i = new ImageView (mContext);  
  96.             //從imgList取得圖片ID   
  97.             i.setImageResource(imgList.get(position).intValue());  
  98.             i.setScaleType(ImageView.ScaleType.FIT_XY);  
  99.             //從imgSizes取得圖片大小   
  100.             int size[]= new int[2];  
  101.             size=(int[]) imgSizes.get(position);  
  102.             i.setLayoutParams(new Gallery.LayoutParams(size[0], size[1]));  
  103.             return i;  
  104.         }  
  105.           
  106.     };  
  107. }  
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.