如何在 Android 上创建照片库?
我想在 Android 上创建一个照片库。我正在使用 Gallery
和 BaseAdapter
创建可滚动图库。现在我想在显示图库图像时添加一个操作(每个图像的宽度与屏幕宽度相同),因此我需要获取其在图像数组中的索引。我的问题是如何获取屏幕上显示的图像的索引?
我想在图像显示在屏幕上时立即获取图像的索引,而不是在单击事件时获取图像的索引。 我尝试在 getView()
中获取当前图像位置,但结果很奇怪:
我滚动到 image02 但position=2(应该是1)。当我向后滚动时,image02 的位置=0(应该是1)。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery g = (Gallery) this.findViewById(R.id.Gallery);
g.setAdapter(new ImageAdapter(this));
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private Integer[] mImageIds = {
//each image's size is 320x60
R.drawable.btm01,
R.drawable.btm02,
R.drawable.btm03
};
public ImageAdapter(Context c){
this.mContext = c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mImageIds.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 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mImageIds[position]);
imageView.setLayoutParams(new Gallery.LayoutParams(320,60));
return imageView;
}
I want to create a photo gallery on Android. I'm using Gallery
and BaseAdapter
to create a scrollable gallery. Now I want to add an action when an image of the gallery is shown (each image's width is the same as screen width), so I need to get its index in the image array. My question is how to get the index of the image which shows on the screen?
I want to get the index of the image as soon as it shows on the screen, not on click event.
I've tried to get current image position in getView()
, but the result is strange:
I scroll to image02 but position=2 (should be 1). When I scroll back, image02's position=0 (should be 1).
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery g = (Gallery) this.findViewById(R.id.Gallery);
g.setAdapter(new ImageAdapter(this));
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private Integer[] mImageIds = {
//each image's size is 320x60
R.drawable.btm01,
R.drawable.btm02,
R.drawable.btm03
};
public ImageAdapter(Context c){
this.mContext = c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mImageIds.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 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mImageIds[position]);
imageView.setLayoutParams(new Gallery.LayoutParams(320,60));
return imageView;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
检查这个示例。
Check this example.