public class MainActivity extends ActionBarActivity {
private GridView gv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gv=(GridView)findViewById(R.id.gridView11);
gv.setAdapter(new ImageAdapter(this));
}
//自定义设配器类
class ImageAdapter extends BaseAdapter{
//定义内容数组,要显示的图片资源
private int[] images={
R.drawable.bg01,R.drawable.bg02,R.drawable.bg03,
R.drawable.bg04,R.drawable.bg05,R.drawable.bg06,
R.drawable.bg07,R.drawable.bg08,R.drawable.bg09,
R.drawable.bg01,R.drawable.bg02,R.drawable.bg03,
R.drawable.bg04,R.drawable.bg05,R.drawable.bg06,
R.drawable.bg07,R.drawable.bg08,R.drawable.bg09
};
private Context c;
public ImageAdapter(Context myContext){
this.c=myContext;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return images.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
ImageView imageView;
if(convertView==null){
imageView=new ImageView(c);
//设置imageView视图的的宽高
imageView.setLayoutParams(new GridView.LayoutParams(85,85));
//设置图片的拉伸方式(截取方式)
imageView.setScaleType(ScaleType.CENTER_CROP);
//设置边距
}else{
imageView=(ImageView)convertView;
}
imageView.setImageResource(images[position]);
return imageView;
}
}
}
private GridView gv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gv=(GridView)findViewById(R.id.gridView11);
gv.setAdapter(new ImageAdapter(this));
}
//自定义设配器类
class ImageAdapter extends BaseAdapter{
//定义内容数组,要显示的图片资源
private int[] images={
R.drawable.bg01,R.drawable.bg02,R.drawable.bg03,
R.drawable.bg04,R.drawable.bg05,R.drawable.bg06,
R.drawable.bg07,R.drawable.bg08,R.drawable.bg09,
R.drawable.bg01,R.drawable.bg02,R.drawable.bg03,
R.drawable.bg04,R.drawable.bg05,R.drawable.bg06,
R.drawable.bg07,R.drawable.bg08,R.drawable.bg09
};
private Context c;
public ImageAdapter(Context myContext){
this.c=myContext;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return images.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
ImageView imageView;
if(convertView==null){
imageView=new ImageView(c);
//设置imageView视图的的宽高
imageView.setLayoutParams(new GridView.LayoutParams(85,85));
//设置图片的拉伸方式(截取方式)
imageView.setScaleType(ScaleType.CENTER_CROP);
//设置边距
}else{
imageView=(ImageView)convertView;
}
imageView.setImageResource(images[position]);
return imageView;
}
}
}