Let's assume that the size of the image of this beautiful mushroom, that I took the other day, is unknown. It could be 350x500 pixels or 1080x1920 or 1920x1080 or 1x1. Any image should be able to used here.
What I'm trying to accomplish is this: I need to stretch the image all the way in the directions where it would meet the edge of the parent view sooner, but not occupy any area in the other two directions.
So here's an example: I have a vertical image and I need to display it on a tablet. In this case it would need to be stretched (as well as maintain aspect ratio) up and down, but not left and right. It is important that the black area on left and right is unoccupied. It should look like this:
The same image would need to be stretched left and right, but not up and down, if it is wider than the parent view:
Here's what I tried:
Setting xml
layout_width
andlayout_height
attributes to different combinations ofmatch_parent
andwrap_content
. That didn't work because we cannot be sure how to stretch the image at compile time because we don't have the dimentions of the image, nor the dimentions of the container.Tried to programmatically stretch the bitmap like this, but that only stretches the width, and is a little inefficient.
It would be nice if there was a way to do it by adjusting the LayoutParams of the ImageView programmatically, based on how the image ratio compares with the parent container ratio. Does anyone know of a clean way to do that?