需要引入
import
android.content.res.Resources;
import
android.content.res.TypedArray;
obtainStyledAttributes 作用就是从我们自己定义的attr.xml读取所需信息
public class ImageAdapter extends BaseAdapter
{
int mGalleryItemBackground;
private Context mContext;
private Integer[] myImageIds;
public ImageAdapter(Context c, Integer[] aid)
{
mContext = c;
myImageIds = aid;
TypedArray a = obtainStyledAttributes(R.styleable.Gallery);
mGalleryItemBackground = a.getResourceId
(
R.styleable.Gallery_android_galleryItemBackground, 0
);
a.recycle();
}
@Override
public int getCount()
{
// TODO Auto-generated method stub
return myImageIds.length;
}
@Override
public Object getItem(int position)
{
// TODO Auto-generated method stub
return null;
}
@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对象*/
ImageView i = new ImageView(mContext);
/*设置图片给imageView对象*/
i.setImageResource(myImageIds[position]);
/*重新设置图片的宽高*/
i.setScaleType(ImageView.ScaleType.FIT_XY);
/*重新设置Layout的宽高*/
i.setLayoutParams(new Gallery.LayoutParams(138, 108));
/*设置Gallery背景图*/
i.setBackgroundResource(mGalleryItemBackground);
/*返回imageView对象*/
return i;
}
}