Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ImageView: Fit Width

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"             /> 
like image 562
Sharj Avatar asked Aug 21 '12 16:08

Sharj


People also ask

How do I stop my android from stretching photos?

You need to set the android:scaleType attribute on your image view. If the image is bigger than your view, either use centerInside or one of the fitXY , fitStart , fitEnd options. You have all the info you need on the Android developer documentation for the ImageView class.

What is adjust view bounds in android?

android:adjustViewBounds—If set to true, the attribute adjusts the bounds of the ImageView control to maintain the aspect ratio of the image displayed through it. android:resizeMode—The resizeMode attribute is used to make a control resizable so we can resize it horizontally, vertically, or around both axes.

What is ImageView in android?

Displays image resources, for example Bitmap or Drawable resources. ImageView is also commonly used to apply tints to an image and handle image scaling.


2 Answers

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.

like image 114
Sharj Avatar answered Oct 16 '22 14:10

Sharj


android:adjustViewBounds="true" does the job!

like image 37
ViliusK Avatar answered Oct 16 '22 13:10

ViliusK