How to fit an image of random size to an ImageView
?
When:
ImageView
dimensions are 250dp * 250dpImageView
dimensions should match scaled image's dimensions after scalingE.g. for an image of 100*150, the image and the ImageView
should be 166*250.
E.g. for an image of 150*100, the image and the ImageView
should be 250*166.
If I set the bounds as
<ImageView
android:id="@+id/picture"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:adjustViewBounds="true" />
images fit properly in the ImageView
, but the ImageView
is always 250dp * 250dp.
May not be answer for this specific question, but if someone is, like me, searching for answer how to fit image in ImageView with bounded size (for example, maxWidth
) while preserving Aspect Ratio and then get rid of excessive space occupied by ImageView, then the simplest solution is to use the following properties in XML:
android:scaleType="centerInside"
android:adjustViewBounds="true"