Reputation: 395
I want to use layout in this way, where image will be loaded from a HTTP server request. I need to know how can I define the image sizes from various android device , as in the server there will not be any drawable asset folder.
Till now I have fixed the height of the Relative Layout inside which the ImageView is placed,which has height and width : fill_parent, and scale_type : fitXY, for which if the image is small its getting stretched.
And this is not a good approach. Can you please suggest me a good way, as how can I define the image sizes.
Upvotes: 1
Views: 73
Reputation: 3076
Good question. There are two solutions that come to my mind:
Similar to how Andorid maintains resources (high display, extra high display, low display, etc.), Request your server to resize photos before sending them
Downloading full resolution photos and resizing them in your app.
Option #1 is more optimized, but #2 is easier to implement. It totally depends on how much time you're willing to spend on this.
It's also highly recommended that you use an image loading library for downloading images. Glide and Picasso are probably two of the best libraries out there. Pass an image URL and they'll handle everything: Downloading the photo, caching it in memory + storage, resizing, etc.
Oh and I don't think fitXY
is the best option for your purpose. Try using centerCrop
which crops your images to fit them properly.
Upvotes: 1