Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scaling image size in Picasso

I want to download an image from a URL and display it in a View in my fragment. I am using Picasso to do this but I want the image to display in it's original aspect ratio but scaled to be a reasonable size - ie 75% of the screen width. I want to support both landscape and portrait photos. So the thinking is something like this.

If the screen width is 200 and the image is landscape 5x4 aspect, then the image should be displayed to 75% width - ie 150 and 120 height (keeping proportions)

If the screen width is 200 and the image is portrait 4x5 aspect, then the image should be display to 75% screen width as the HEIGHT dimension - ie 150 height and 120 width

How can I do this using Picasso?

like image 861
NZJames Avatar asked Feb 13 '23 11:02

NZJames


1 Answers

Breaking the first part down in to two sections:

I want the image to display in it's original aspect ratio

To do this you can call .fit().centerInside() on the Picasso request

scaled to be a reasonable size - ie 75% of the screen width

The easiest way to do this would be to specify this in your layout, so set an ImageView to be this width. Then when Picasso loads the image into this ImageView it will automatically scale it to fit (but keeping the aspect ratio as specified above).

like image 103
Michael Avatar answered Feb 15 '23 01:02

Michael