Fit image into ImageView, keep aspect ratio and then resize ImageView to image dimensions?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
On Android, fitting an image into an ImageView while keeping its aspect ratio is easy to describe and surprisingly easy to get wrong in code. The two goals are separate: first scale the bitmap without distortion, then size the ImageView to match the displayed result. The clean solution is to measure the available width, compute the corresponding height from the bitmap's ratio, and update the view's layout params once that size is known.
Use The Right ImageView Settings
If the ImageView itself is allowed to change size, start with layout attributes that let Android respect the image ratio.
adjustViewBounds="true" tells the view that its bounds may change to preserve the drawable ratio. fitCenter ensures the full image is visible without cropping.
This is often enough when the parent provides a known width and the ImageView only needs to grow vertically.
Resize The View After You Know The Available Width
When you want precise control, calculate the displayed size yourself after the parent layout has been measured.
This code waits until the ImageView has a real width, then computes the matching height from the bitmap's aspect ratio.
Why Resizing Before Layout Usually Fails
A common bug is reading imageView.width too early, such as in onCreate, before the layout pass has happened. At that point the width is often 0, which leads to incorrect math or no visible resize.
Using doOnLayout, post, or a layout listener avoids that timing problem.
If you are loading images asynchronously from the network, perform the size update after the bitmap or drawable has actually arrived. The view size and the image size both need to be known before the final layout can be correct.
Avoid Distortion And Unnecessary Memory Use
If the source bitmap is extremely large, do not decode it at full size unless you actually need that resolution. You can downsample during decoding so the bitmap roughly matches the maximum display size.
This helps avoid memory pressure while still preserving the correct display ratio.
When scaleType Changes The Result
If your goal is to show the whole image, use fitCenter or centerInside. If you use centerCrop, Android will intentionally crop the image to fill the bounds, which means the ImageView size and the visible image content no longer mean the same thing.
That is a common source of confusion. The aspect ratio may still be preserved, but part of the bitmap can disappear by design.
Common Pitfalls
The most common mistake is measuring the ImageView before layout has finished. If the width is still zero, the computed height will be wrong.
Another issue is using the wrong scaleType. centerCrop is great for cover-style thumbnails, but it is the wrong choice when you need the full image visible.
Developers also often forget that large bitmaps should be downsampled before display. Correct aspect ratio does not protect you from memory problems.
Finally, avoid hard-coding both width and height unless you deliberately want distortion. If the ratio matters, compute one dimension from the other.
Summary
- Use
adjustViewBoundsand an appropriatescaleTypeto preserve aspect ratio. - Resize the
ImageViewonly after the layout width is known. - Compute the target height from the bitmap ratio instead of guessing it.
- Downsample very large images to avoid unnecessary memory use.
- Choose
fitCenterorcenterInsidewhen the whole image must remain visible.

