I need to resize an ImageView such that it fits to the screen, maintains the same aspect ratio. The following conditions hold:
- Images are a fixed width (size of the width of screen)
- Images are downloaded from the Internet, i.e. size can only be determined later
- Aspect ratio for each image will vary; some are tall, some are square, some are flat
- ImageView's height needs to change, either bigger or smaller based on the aspect ratio of the
- Excess height is cropped, can't have a lot of empty space like it is by default.
For example, a tiny 50×50 image on a 400 px width screen would scale the image up to 400×400 px. A 800×200 image would scale to 400×100.
I've looked at most of the other threads, and many proposed solutions like simply changing adjustViewBounds
and scaleType
will only scale down an image, and not scale it up.
Best Answer
I created it with the help of this Bob Lee's answer in this post: Android: How to stretch an image to the screen width while maintaining aspect ratio?
Now to use it in the XML:
Have fun!