Search code examples
androidimageviewscale

Android Scaling Image


I have a Relative layout whose height is fixed - 356 px and width is wrap content.
I have placed an Imageview with background as image .
The image dimensions are 496 px * 496 px.

The image is a square image but getting scaled as a rectangle .
Here is the XML-

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rlMain"
android:layout_width="wrap_content"
android:layout_height="356px" >

<ImageView
    android:id="@+id/ivIcon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/outdoor_verygood_ring" />

</RelativeLayout>

Here is how the original image is - enter image description here

Here is how it looks in the Relative layout -

enter image description here

What i want is - if the image is getting scaled it should get scaled in both X and Y .
How can i achieve this ?


Solution

  • src never change the aspect ratio. it displays the image as it is. by leaving extra space around the Image view.

    On he other hand background try to fill the full background of the ImageView. As you give the constant height and width with wrap_content. So in your case first it wraps the width and then apply the height. which is obviously wrong.

    In you case if you want 1:1 ratio you can give the fix width and height. where width=height.

    But in general if you want any aspect ratio to maintain then you can use src attribute.

    and

    android:adjustViewBounds="true" 
    

    is a magical line.