Android的Gallery控件是个很不错的看图控件,大大减轻了开发者对于看图功能的开发,而且效果也比较美观。本文介绍Gallery的用法,用反射机制来动态读取资源中的图片。
<?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>
本文的效果图:
main.xml源码:
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <Galleryandroid:id="@+id/gallery"android:layout_height="fill_parent"android:layout_width="fill_parent"></Gallery>
- </LinearLayout>
程序源码:
- packagecom.testImageView;
- importjava.lang.reflect.Field;
- importjava.util.ArrayList;
- importandroid.app.Activity;
- importandroid.content.Context;
- importandroid.graphics.Bitmap;
- importandroid.graphics.BitmapFactory;
- importandroid.os.Bundle;
- importandroid.view.View;
- importandroid.view.ViewGroup;
- importandroid.widget.AdapterView;
- importandroid.widget.BaseAdapter;
- importandroid.widget.Gallery;
- importandroid.widget.ImageView;
- importandroid.widget.AdapterView.OnItemClickListener;
- publicclasstestImageViewextendsActivity{
- privateGallerymGallery;
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mGallery=(Gallery)findViewById(R.id.gallery);
- try{
- mGallery.setAdapter(newImageAdapter(this));
- }catch(IllegalArgumentExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }catch(IllegalAccessExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }
- mGallery.setOnItemClickListener(newOnItemClickListener(){
- publicvoidonItemClick(AdapterViewparent,Viewv,intposition,longid){
- testImageView.this.setTitle(String.valueOf(position));
- }
- });
- }
- /*
- *classImageAdapterisusedtocontrolgallerysourceandoperation.
- */
- privateclassImageAdapterextendsBaseAdapter{
- privateContextmContext;
- privateArrayList<Integer>imgList=newArrayList<Integer>();
- privateArrayList<Object>imgSizes=newArrayList<Object>();
- publicImageAdapter(Contextc)throwsIllegalArgumentException,IllegalAccessException{
- mContext=c;
- //用反射机制来获取资源中的图片ID和尺寸
- Field[]fields=R.drawable.class.getDeclaredFields();
- for(Fieldfield:fields)
- {
- if(!"icon".equals(field.getName()))//除了icon之外的图片
- {
- intindex=field.getInt(R.drawable.class);
- //保存图片ID
- imgList.add(index);
- //保存图片大小
- intsize[]=newint[2];
- BitmapbmImg=BitmapFactory.decodeResource(getResources(),index);
- size[0]=bmImg.getWidth();size[1]=bmImg.getHeight();
- imgSizes.add(size);
- }
- }
- }
- @Override
- publicintgetCount(){
- //TODOAuto-generatedmethodstub
- returnimgList.size();
- }
- @Override
- publicObjectgetItem(intposition){
- //TODOAuto-generatedmethodstub
- returnposition;
- }
- @Override
- publiclonggetItemId(intposition){
- //TODOAuto-generatedmethodstub
- returnposition;
- }
- @Override
- publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
- //TODOAuto-generatedmethodstub
- ImageViewi=newImageView(mContext);
- //从imgList取得图片ID
- i.setImageResource(imgList.get(position).intValue());
- i.setScaleType(ImageView.ScaleType.FIT_XY);
- //从imgSizes取得图片大小
- intsize[]=newint[2];
- size=(int[])imgSizes.get(position);
- i.setLayoutParams(newGallery.LayoutParams(size[0],size[1]));
- returni;
- }
- };
- }