Android ImageView: Fit Width

Sharj picture Sharj · Aug 21, 2012 · Viewed 93.6k times · Source

I download image and set it as a screen background dynamically using Imageview. I have tried ScaleType, to scale the image.

If image height is larger than width then ScaleTypes fitStart, fitEnd and fitCenter don't work. Android scale down the photo and fit it based on the height, but I see some extra blank space as part of the width.

I want to scale down the photo based on the width so that it fits the width and I don't care if there's some extra blank space as part of the height or if height is too long it is fine if it's going out of the view(if that's possible?).

ScaleType.XY scale the photo and fit everything in the ImageView and doesn't care about image height/weight ratio.

<ImageView 
                android:id="@+id/background"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:adjustViewBounds="true"
                android:scaleType="fitStart"
            />

Answer

Sharj picture Sharj · Apr 3, 2013

I ended up using this code:

<ImageView 
                android:id="@+id/background"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:adjustViewBounds="true"
                android:scaleType="centerCrop"
                android:src="@drawable/name"
            />

Make sure you set the image using src instead of background.