在Android 开发中遇到Gallery控件
Gallery gallery = (Gallery) findViewById(R.id.mygallery);
List<String>picList = new ArrayList<String>();
picList = getPictures();
gallery.setAdapter(new ImageAdapter(this,nameList));
//设置第二副图片居中显示,这样第一副就会在第二副左边显示,
调整合适的尺寸就能实现屏幕左边开始显示第一张。
gallery.setSelection(1);
private List<String> getPictures()
{
得到图片文件路径
return it;
}
public class ImageAdapter extends BaseAdapter{
/*声明变量*/
private int gItemBg;
private Context mContext;
private List<String> lis;
public ImageAdapter(Context c,List<String> li)
{
mContext = c;
lis=li;
/* 使用在res/values/attrs.xml中的<declare-styleable>定义
* 的Gallery属性.*/
TypedArray a = obtainStyledAttributes(R.styleable.Gallery);
/*取得Gallery属性的Index id*/
gItemBg = a.getResourceId
(R.styleable.Gallery_android_galleryItemBackground, 0);
gItemBg = a.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0);
/*让对象的styleable属性能够反复使用*/
a.recycle();
}
public int getCount() {
// TODO Auto-generated method stub
return lis.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView i = new ImageView(mContext);
// 设置图片给ImageView对象
Bitmap bm = BitmapFactory.decodeFile(lis.
get(position).toString());
i.setImageBitmap(bm);
// 重新设置图片的宽高
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
// 重新设置Layout的宽高
i.setLayoutParams(new Gallery.LayoutParams(150,120));
/*设置Gallery背景图*/
// i.setBackgroundResource(gItemBg);
return i;
}
}