3

I've got a GridView full of images. Here's my custom adapter:

    public class ImageAdapter extends ArrayAdapter<String> {

    private final String LOG_TAG = ImageAdapter.class.getSimpleName();

    public ImageAdapter(Activity context, List<String> urls){
        super(context, 0, urls);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        String url = getItem(position);
        View rootView = LayoutInflater.from(getContext()).inflate(R.layout.grid_item, parent, false);

        ImageView img = (ImageView) rootView.findViewById(R.id.grid_view_item);
        img.setLayoutParams(
                new GridView.LayoutParams(
                        GridView.AUTO_FIT,
                        500));
        img.setScaleType(ImageView.ScaleType.FIT_XY);

        Picasso.with(getContext())
                .load(url)
                .into(img);

        return rootView;
    }
}


You can see that I set the height to be 500. But I don't want this.
I want the height to be the actual height of the image, just like the width.
How can I do that?

Aakash
  • 5,181
  • 5
  • 20
  • 37
Cas
  • 336
  • 1
  • 3
  • 18
  • You should get the image directly then set the width in the `ImageView` http://stackoverflow.com/questions/25522847/getting-image-width-and-height-with-picasso-library – Victory Sep 24 '15 at 22:43
  • @Victory When I try to put the Picasso get inside a Bitmap variable (Bitmap image = Picasso.with(getContext()).load(url).get();) I get an IOException. – Cas Sep 24 '15 at 23:00
  • What happens when you set `android:adjustViewBounds="true"` on the `ImageView`? – kris larson Sep 24 '15 at 23:01
  • @krislarson Well, it make the image fits in the ImageView, but not changes the ImageView's size. Basically it distorts the image. ;/ – Cas Sep 24 '15 at 23:07
  • Are you also setting height to `wrap_content` on the `ImageView`? – kris larson Sep 24 '15 at 23:17
  • @krislarson Oh, that works! Thanks man! – Cas Sep 24 '15 at 23:23
  • Great! I'm putting that into an answer for you. – kris larson Sep 24 '15 at 23:24

1 Answers1

1

Use adjustViewBounds property to force the height to the correct dimension.

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true" />
kris larson
  • 30,387
  • 5
  • 62
  • 74