简体   繁体   中英

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 - 在此处输入图片说明

Here is how it looks in the Relative layout -

在此处输入图片说明

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

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM