代码之家  ›  专栏  ›  技术社区  ›  Spredzy

Gallery and fullscreen ImageView, problem in putting them together

  •  6
  • Spredzy  · 技术社区  · 15 年前

    我会尽量说清楚。

    Here What I am trying to do

    For this purspose and for a better user experience, I thought of a Gallery. Untill there everything is OK!

    问题

    问题是,在适配器的getview函数中,它只使用我在库中的第一个图像的gallery.layoutParams。也就是说,如果第一张图片是一幅风景画,而第二张图片是一幅肖像画,那么第二张图片将显示为风景画,与第一张图片的尺寸相同。我确实重置了gallery.layoutParams,但这并不重要,它仍然具有第一个ImageView的layoutParams。

    代码

    public View getView(int position, View convertView, ViewGroup parent) {
    
            ImageView im = new ImageView(mContext);
    
            Bitmap bm = BitmapFactory.decodeByteArray(gallery.get(position).mContent, 0, gallery.get(position).mContent.length);
            im.setImageBitmap(bm);
    
            int width = 0;
            int height = 0;
    
            if (bm.getHeight() < bm.getWidth()) {
                width = getWindowManager().getDefaultDisplay().getWidth();
                height = bm.getHeight() * width / bm.getWidth();
            }
            else {
                height = getWindowManager().getDefaultDisplay().getHeight();
                width = bm.getWidth() * height/ bm.getHeight();
                    }
    
            Gallery.LayoutParams lp = new Gallery.LayoutParams(width, height);
            im.setLayoutParams(lp);
    
            return im;
    }
    

    If any of you see why I would be really please to know the answer,

    1 回复  |  直到 15 年前
        1
  •  4
  •   Romain Guy    15 年前

    Gallery forces all children to have the same size. Also, you are creating a new View every time getView() is called and it's a terrible idea. You should use the convertView when it's not null. There's currently a missing feature in Gallery so convertView is always null, but to benefit from it in a future version of Android, use the convertView anyway. It's also a good practice that you must apply in every Adapter you write.