如何在Android上创建照片库?
我想在Android上创建一个照片库。我正在使用如何在Android上创建照片库?,android,image-gallery,Android,Image Gallery,我想在Android上创建一个照片库。我正在使用Gallery和BaseAdapter创建一个可滚动的库。现在我想添加一个动作,当画廊的图像显示时(每个图像的宽度与屏幕宽度相同),所以我需要在图像数组中获取其索引。我的问题是如何获取屏幕上显示的图像的索引 我希望在图像显示在屏幕上时立即获取其索引,而不是单击事件。 我尝试在getView()中获取当前图像位置,但结果很奇怪: 我滚动到image02,但位置=2(应该是1)。当我向后滚动时,image02的位置=0(应该是1) 检查示例 我希望在图
Gallery
和BaseAdapter
创建一个可滚动的库。现在我想添加一个动作,当画廊的图像显示时(每个图像的宽度与屏幕宽度相同),所以我需要在图像数组中获取其索引。我的问题是如何获取屏幕上显示的图像的索引
我希望在图像显示在屏幕上时立即获取其索引,而不是单击事件。
我尝试在getView()
中获取当前图像位置,但结果很奇怪:
我滚动到image02,但位置=2(应该是1)。当我向后滚动时,image02的位置=0(应该是1)
检查示例
我希望在图像显示时立即获取其索引,而不是单击事件。这可能吗?我认为索引应该从onScroll事件返回。但是我找不到任何可以使用的函数。@iPhoney:你为什么需要它?这对我来说没有任何意义,但您可以在
getView()
方法中使用position
。简言之,我想通知用户,图像[0x]一出现在屏幕上就会显示出来。当用户点击它的时候就不会了。所以我在寻找类似“scrollDidEndAtIndex(int index)”的东西。哦,ListView
和Galleries
中的动画是我的待办事项列表中的一部分。这是我举的一个简单的例子:
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;
}
// Reference the Gallery view
Gallery g = (Gallery) this.findViewById(R.id.Gallery);
// Set the adapter to our custom adapter (below)
g.setAdapter(new ImageAdapter(this));
// Set a item click listener, and just Toast the clicked position
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
Toast.makeText(Gallery1.this, "" + position, Toast.LENGTH_SHORT).show();
}
});