国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android入門之Gallery用法實例解析

2020-04-11 11:46:34
字體:
來源:轉載
供稿:網友

本文實例介紹的Android的Gallery控件是個很不錯的看圖控件,可以大大減輕開發者對于看圖功能的開發,并且效果也很美觀。本文實例中的Gallery的用法,主要實現用反射機制來動態讀取資源中的圖片。

該實例代碼運行的效果圖如下:

 

main.xml源碼如下:

<?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>

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) {       // TODO Auto-generated catch block       e.printStackTrace();     } catch (IllegalAccessException e) {       // TODO Auto-generated catch block       e.printStackTrace();     }     mGallery.setOnItemClickListener(new OnItemClickListener() {       public void onItemClick(AdapterView parent, View v, int position, long id) {         testImageView.this.setTitle(String.valueOf(position));       }     });   }      /*    * class ImageAdapter is used to control gallery source and operation.    */   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);           //保存圖片ID           imgList.add(index);           //保存圖片大小           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() {       // TODO Auto-generated method stub        return imgList.size();     }      @Override     public Object getItem(int position) {       // TODO Auto-generated method stub        return position;     }      @Override     public long getItemId(int position) {       // TODO Auto-generated method stub        return position;     }      @Override     public View getView(int position, View convertView, ViewGroup parent) {       // TODO Auto-generated method stub        ImageView i = new ImageView (mContext);       //從imgList取得圖片ID       i.setImageResource(imgList.get(position).intValue());       i.setScaleType(ImageView.ScaleType.FIT_XY);       //從imgSizes取得圖片大小       int size[]= new int[2];       size=(int[]) imgSizes.get(position);       i.setLayoutParams(new Gallery.LayoutParams(size[0], size[1]));       return i;     }        }; }
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 武鸣县| 阿克陶县| 牡丹江市| 怀宁县| 涟源市| 东源县| 石河子市| 壶关县| 海城市| 宁津县| 淮南市| 阿鲁科尔沁旗| 泌阳县| 河西区| 高密市| 平南县| 四子王旗| 开鲁县| 和林格尔县| 尖扎县| 旌德县| 新巴尔虎右旗| 蓬莱市| 中方县| 高平市| 井研县| 德安县| 沙田区| 聊城市| 塔河县| 揭西县| 武平县| 叶城县| 藁城市| 双鸭山市| 濮阳县| 黔西县| 阳东县| 黔西| 静宁县| 厦门市|