Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to expand rectangular image with same ratio to fit the screen

I have the following rectangular image to be set in my application.

enter image description here

Now I am trying to expand the image with same ratio on both sides until it fits to the screen.But I cant get the expected image.

enter image description here

So far my code is :

<ImageView
    android:id="@+id/imgid"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:src="@drawable/shayneward"
    android:scaleType="fitXY"/>

It dont care that the image gets cut off, but it should expand on both sides with same ratio until it fits to the screen.

like image 520
Dileep Perla Avatar asked Dec 07 '22 13:12

Dileep Perla


2 Answers

Change android:scaleType="fitXY" to android:scaleType="centerCrop" to get the behavior that you want.

The different scaling options are documented under ImageView.ScaleType.

like image 61
Darshan Rivka Whittle Avatar answered May 01 '23 08:05

Darshan Rivka Whittle


Try using this:

android:scaleType="centerCrop"

From the Android documentation, center crop does the following:

Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or larger than the corresponding dimension of the view (minus padding).

See documentation for ImageView.ScaleType here.

like image 22
happydude Avatar answered May 01 '23 09:05

happydude