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" />
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.
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.
Displays image resources, for example Bitmap or Drawable resources. ImageView is also commonly used to apply tints to an image and handle image scaling.
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
.
android:adjustViewBounds="true"
does the job!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With