返回信息流自己做了个东西需要用到Gallery。。前几天好不容易成功运行了。后来扩展了下应用突然发现gallery用不了了。。
然后开始研究android.com上面的教程和API文档。。。
不过我还是看不懂。话说gallery是怎么现实gallery里面现实图片组的。
找不到具体的实现这个功能的代码。。= =
我把android.com的教程代码贴过来把。。
主程序部分:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery gallery = (Gallery) findViewById(R.id.gallery);
gallery.setAdapter(new ImageAdapter(this));
//还有就是这里的context是指向什么。的。。。。上次看的教程里面是这样写的
/*
ImageView chosen = (ImageView) findViewById(R.id.chosen);
ImageAdapter a= new ImageAdapter(chosen.getContext());
gallery.setAdapter(a);
*/
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
Toast.makeText(HelloGallery.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
Imageadapter部分
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
private Integer[] mImageIds = {
R.drawable.sample_1,
R.drawable.sample_2,
R.drawable.sample_3,
R.drawable.sample_4,
R.drawable.sample_5,
R.drawable.sample_6,
R.drawable.sample_7
};
public ImageAdapter(Context c) {
mContext = c;
TypedArray attr = mContext.obtainStyledAttributes(R.styleable.HelloGallery);
mGalleryItemBackground = attr.getResourceId(
R.styleable.HelloGallery_android_galleryItemBackground, 0);
attr.recycle();
}
public int getCount() {
return mImageIds.length;
}
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 = new ImageView(mContext);
imageView.setImageResource(mImageIds[position]);
imageView.setLayoutParams(new Gallery.LayoutParams(150, 100));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setBackgroundResource(mGalleryItemBackground);
return imageView;
}
}
下面是关于这个代码的注释
First, there are a few member variables, including an array of IDs that reference the images saved in the drawable resources directory (res/drawable/).
Next is the class constructor, where the Context for an ImageAdapter instance is defined and the styleable resource defined in the last step is acquired and saved to a local field. At the end of the constructor, recycle() is called on the TypedArray so it can be re-used by the system.
The methods getCount(), getItem(int), and getItemId(int) are methods that must be implemented for simple queries on the Adapter. The method does the work to apply an image to an {@link android.widget.ImageView that will be embedded in the Gallery. In this method, the member Context is used to create a new ImageView. The ImageView is prepared by applying an image from the local array of drawable resources, setting the Gallery.LayoutParams height and width for the image, setting the scale to fit the ImageView dimensions, and then finally setting the background to use the styleable attribute acquired in the constructor.
See ImageView.ScaleType for other image scaling options.
Run the application.
纠结死了
这是一条镜像帖。来源:北邮人论坛 / mobile-terminal-at / #6111同步于 2012/4/23
该镜像源已超过 30 天没有更新,可能在源站已被删除。
MobileTerminalAT机器人发帖
求助属性---如何使用Gallery
EastDon
2012/4/23镜像同步1 回复
订阅后,新回复会通过你的通知中心匿名送达。
1 条回复