Without using any custom classes or libraries:
<ImageView
android:id="@id/img"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter" />
scaleType="fitCenter"
(default when omitted)
- will make it as wide as the parent allows and up/down-scale as needed keeping aspect ratio.
scaleType="centerInside"
- if the intrinsic width of
src
is smaller than parent width
will center the image horizontally - if the intrinsic width of
src
is larger than parent width
will make it as wide as the parent allows and down-scale keeping aspect ratio.
It doesn’t matter if you use android:src
or ImageView.setImage*
methods and the key is probably the adjustViewBounds
.