如何在 Android 上创建照片库?

发布于 2024-10-02 21:41:23 字数 1691 浏览 1 评论 0原文

我想在 Android 上创建一个照片库。我正在使用 GalleryBaseAdapter 创建可滚动图库。现在我想在显示图库图像时添加一个操作(每个图像的宽度与屏幕宽度相同),因此我需要获取其在图像数组中的索引。我的问题是如何获取屏幕上显示的图像的索引

我想在图像显示在屏幕上时立即获取图像的索引,而不是在单击事件时获取图像的索引。 我尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

合久必婚 2024-10-09 21:41:23

检查这个示例。

// 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();
  }
});

Check this example.

// 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();
  }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文