4、适配器代码如下:
- public class ImageAdapter extends BaseAdapter {
- int mGalleryItemBackGround;
- private Context mContext;
- private List<Drawable> list;
- public ImageAdapter(Context mContext,List<Drawable> li) {
- this.mContext = mContext;
- this.list = li;
- TypedArray a = mContext.obtainStyledAttributes(R.styleable.Gallery1);
- mGalleryItemBackGround = a.getResourceId(R.styleable.Gallery1_Android_galleryItemBackground, 0);
- a.recycle();
- }
- public int getCount() {
- return list.size();
- }
- public Object getItem(int position) {
- return position;
- }
- public long getItemId(int position) {
- return position;
- }
- public View getView(int position, View convertView, ViewGroup parent) {
- // 声明一个ImageView对象
- ImageView i = new ImageView(mContext);
- BitmapFactory.Options options = new BitmapFactory.Options();
- //设置图片的大小,宽高都为原来的二分之一,即图片为原来的四分之一
- options.inSampleSize = 2;
- i.setImageDrawable(list.get(position));
- //设置layout的宽高
- i.setLayoutParams(new Gallery.LayoutParams(200,200));//layout
- //重新设置图片的宽高
- i.setScaleType(ImageView.ScaleType.FIT_XY);//set scale type
- //设置图片的背景,即自定义的背景风格
- i.setBackgroundResource(mGalleryItemBackGround);
- return i;
- }
- class ViewHolder{
- ImageView image;
- }
- }
效果如下:

