Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scaling an image to max available width, keeping aspect ratio and avoiding ImageView extra space

I have this simple layout XML, and a green_square.png file which is 30X30 pixels

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:background="@color/red"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:adjustViewBounds="true"
        android:scaleType="fitStart"
        android:src="@drawable/green_square"/>

</LinearLayout>

What I'm trying to achieve is scaling the image to fill the parent width, keeping it's aspect ratio with no extra space of the ImageView.

I've added a red background to the ImageView just for reference of the extra space I want to avoid.

Here is my goal

like image 235
Shlomi Schwartz Avatar asked Jan 03 '12 10:01

Shlomi Schwartz


People also ask

Which attribute is important for ImageView other than width and height *?

Bookmark this question.

What is aspect ratio of an image?

What is the image aspect ratio? The aspect ratio is the proportional relationship between an image's width and height. Different cameras shoot in different aspect ratios, so whether you're using a digital camera, a 35mm film camera, or an iPhone, they can all vary.


1 Answers

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

this works for me. Full width of parent and center crop to keep aspect ratio

like image 79
Blucreation Avatar answered Oct 07 '22 14:10

Blucreation