Anukool
Anukool

Reputation: 5391

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 ?

Upvotes: 1

Views: 1581

Answers (3)

Mohsin Naeem
Mohsin Naeem

Reputation: 12642

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.

Upvotes: 2

Siddharth
Siddharth

Reputation: 9584

You need to fix this

From

android:background="@drawable/outdoor_verygood_ring"

To

android:src="@drawable/outdoor_verygood_ring"
android:adjustViewBounds="true"

Background is resized based on the widget placement and its properties. The src, is within the ImageView and is controlled by the ImageView properties. No ImageView property controls the background.

Upvotes: 0

Sushil
Sushil

Reputation: 8488

You need to set the aspect ratio of the image properly. In your case anyway try different options given for imageview.scaleType. Follow this link below to set proper option for you in xml:

May be in you case try:

android:scaleType= "CENTER_INSIDE" for imageview

Upvotes: 0

Related Questions