Gallery 是显示图片用的,需要指定一个图片的来源 adapter ,这个继承自 BaseAdapter ,具体代码如下
xml 代码
<? xml version="1.0" encoding="utf-8" ?> < Gallery xmlns:android ="http://schemas.android.com/apk/res/android" android:id ="@+id/gallery" android:layout_width ="fill_parent" android:layout_height ="fill_parent" > </ Gallery >
java 代码
package zziss.android.gallerytest; import android.app.Activity; import android.app.AlertDialog; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.Gallery; public class GalleryTestActivity extends Activity { /** Called when the activity is first created. */ private ImageAdapter2 ia; private Gallery iGallery; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); iGallery = (Gallery) this.findViewById(R.id.gallery); try { ia = new ImageAdapter2( this); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } iGallery.setBackgroundResource(R.drawable.aa); iGallery.setAdapter(ia); iGallery.setOnItemClickListener( new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { AlertDialog dlg = new AlertDialog.Builder(GalleryTestActivity. this) .setTitle("Gallery 测试") .setMessage("点击了"+Integer.toString(arg2)+"图片") // .setIcon(ia.mImageIds[arg2]) .setPositiveButton("确定", null) .create(); dlg.setCanceledOnTouchOutside( false); dlg.show(); } }); } }
ImageAdapter代码
package zziss.android.gallerytest; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView; public class ImageAdapter extends BaseAdapter { private Context context; public Integer[] mImageIds= { R.drawable.phone, R.drawable.tetris, R.drawable.tetromino, R.drawable.text, R.drawable.videos }; public ImageAdapter(Context context) { this.context = context; } @Override public int getCount() { // TODO Auto-generated method stub return mImageIds.length; } @Override public Object getItem( int arg0) { // TODO Auto-generated method stub return arg0; } @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 iv = new ImageView(context); iv.setImageResource(mImageIds[position]); iv.setLayoutParams( new Gallery.LayoutParams(60,60)); iv.setScaleType(ImageView.ScaleType.FIT_CENTER); return iv; } }
ImageAdater2代码
这个是从 drawable 中获取的所有图片,不需要编码指定图片资源
package zziss.android.gallerytest; import java.lang.reflect.Field; import java.util.ArrayList; import zziss.android.gallerytest.R; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView; public class ImageAdapter2 extends BaseAdapter { private ArrayList<Integer> imgList; private ArrayList<Object> sizeList; private Context context; public ImageAdapter2(Context context) throws IllegalArgumentException, IllegalAccessException { this.context = context; imgList = new ArrayList<Integer>(); sizeList = new ArrayList<Object>(); Field[] fields=R.drawable. class.getDeclaredFields(); for(Field field:fields) { if (!("icon".equals(field.getClass()))) { int index = field.getInt(R.drawable. class); imgList.add(index); int size[]= new int[2]; Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), index); size[0] = bitmap.getWidth(); size[1] = bitmap.getHeight(); sizeList.add(size); bitmap= null; } } } @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 iv = new ImageView(context); iv.setImageResource(imgList.get(position).intValue()); iv.setScaleType(ImageView.ScaleType.FIT_XY); int size[] = new int[2]; size = ( int[])sizeList.get(position); iv.setLayoutParams( new Gallery.LayoutParams(size[0],size[1])); return iv; } }