多的不多说了,直接上代码,也是有注视的,感谢 魏祝林
package com.testSerfaceView;
import com.testSerfaceView.R.drawable;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
public class GalleryDemo extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grallery);
((Gallery) findViewById(R.id.myGallery1)).setAdapter(new ImageAdapter(
this));
}
public class ImageAdapter extends BaseAdapter {
/* 类成员 myContext为Context父类 */
private Context myContext;
/* 使用res/drawable图片作为图片来源 */
private int[] myImageIds = { drawable.andriod0001,
drawable.andriod0002, drawable.andriod0003,
drawable.andriod0004, drawable.andriod0005,
drawable.andriod0006, drawable.andriod0007,
drawable.andriod0008, drawable.andriod0009,
drawable.andriod0010, drawable.andriod0011,
drawable.andriod0012, drawable.andriod0013,
drawable.andriod0014, drawable.andriod0015,
drawable.andriod0016};
/* 构造器只有一个参数,即要存储的Context */
public ImageAdapter(Context c) {
this.myContext = c;
}
/* 返回所有已定义的图片总数量 */
public int getCount() {
// return this.myImageIds.length;
return Integer.MAX_VALUE;
}
/* 利用getItem方法,取得目前容器中图像的数组ID */
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
/* 取得目前欲显示的图像View,传入数组ID值使之读取与成像 */
public View getView(int position, View convertView, ViewGroup parent) {
/* 创建一个ImageView对象 */
ImageView i = new ImageView(this.myContext);
i.setImageResource(this.myImageIds[position % myImageIds.length]);
i.setScaleType(ImageView.ScaleType.FIT_XY);
/* 设置这个ImageView对象的宽高,单位为dip */
i.setLayoutParams(new Gallery.LayoutParams(120, 120));
return i;
}
}
}

