Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gallery and fullscreen ImageView, problem in putting them together

I will try to be as clear as possible.

Here What I am trying to do

An Image Viewer, where Image would be display as big as they can on the device screen. For this purspose and for a better user experience, I thought of a Gallery. Untill there everything is OK!

The problem

The problem is that in the function getView of my Adapter, it uses only the Gallery.LayoutParams of the first Image I have in my Gallery. Which means that if the first picture is a landscape and the second one a portrait the second one will be display as landscape, with the same dimension as the first one. I do reset the Gallery.LayoutParams but it does not matter, it still have the LayoutParams of the first ImageView.

The code

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,

like image 862
Spredzy Avatar asked Jun 26 '10 21:06

Spredzy


1 Answers

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.

like image 120
Romain Guy Avatar answered Nov 15 '22 05:11

Romain Guy