ImageView
aspect ratio
image resizing
fit image
Android development

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.

xml
1<ImageView
2    android:id="@+id/photoView"
3    android:layout_width="match_parent"
4    android:layout_height="wrap_content"
5    android:adjustViewBounds="true"
6    android:scaleType="fitCenter" />

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.

kotlin
1import android.graphics.Bitmap
2import android.view.ViewGroup
3import android.widget.ImageView
4import androidx.core.view.doOnLayout
5import kotlin.math.roundToInt
6
7fun setBitmapKeepingAspectRatio(imageView: ImageView, bitmap: Bitmap) {
8    imageView.doOnLayout {
9        val availableWidth = imageView.width
10        if (availableWidth == 0) return@doOnLayout
11
12        val ratio = bitmap.height.toFloat() / bitmap.width.toFloat()
13        val targetHeight = (availableWidth * ratio).roundToInt()
14
15        val params = imageView.layoutParams
16        params.width = availableWidth
17        params.height = targetHeight
18        imageView.layoutParams = params
19        imageView.setImageBitmap(bitmap)
20    }
21}

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.

kotlin
1import android.graphics.BitmapFactory
2
3fun decodeScaledBitmap(path: String, reqWidth: Int, reqHeight: Int) = BitmapFactory.Options().run {
4    inJustDecodeBounds = true
5    BitmapFactory.decodeFile(path, this)
6
7    var sampleSize = 1
8    while ((outWidth / sampleSize) > reqWidth || (outHeight / sampleSize) > reqHeight) {
9        sampleSize *= 2
10    }
11
12    inJustDecodeBounds = false
13    inSampleSize = sampleSize
14    BitmapFactory.decodeFile(path, this)
15}

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 adjustViewBounds and an appropriate scaleType to preserve aspect ratio.
  • Resize the ImageView only 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 fitCenter or centerInside when the whole image must remain visible.

Course illustration
Course illustration

All Rights Reserved.