11

Can you help me to configure my layout? I don't achieve what I'm looking for:

I've put the imageview background to red for explain what I want:

The image's width is smaller than the layout width. I'd like the image grow proportionally keeping its aspect ratio until reach the aspect at the first image (I modified it manually with photoshop) but when setting width="fill_parent" the result is the second image and setting both width and height to "fill_parent" the result is the third image.

I tried to combine with ScaleType with no success

How can I achieve the first option? Thanks

enter image description here

Addev
  • 31,819
  • 51
  • 183
  • 302

2 Answers2

14

Use android:adjustViewBounds=true attr in xml for ImageView


This question is solved here by extending the ImageView class.

Android ImageView adjusting parent's height and fitting width


I created a sample. It works for me. Here is the xml layout file.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ImageView android:src="@drawable/hydrangeas"
        android:layout_height="wrap_content" android:id="@+id/imageView1"
        android:scaleType="centerInside"
        android:adjustViewBounds="true"
        android:background="@android:color/white"
        android:layout_width="wrap_content"/>

</LinearLayout>

It works for fitCenter too. Have a look at the snapshot.
With centerInside and adjustViewBounds="true"

Community
  • 1
  • 1
Ron
  • 24,175
  • 8
  • 56
  • 97
  • I really don't understand how adjustViewBounds works but I dont reach my target with it can you explain a bit more how to do it? – Addev Aug 12 '11 at 14:41
  • try using scaletype=centerInside along with it. – Ron Aug 12 '11 at 15:19
  • 1
    Hi, It is not working also in my case. Smaller image is not upscaled to fit the parent view, but som borders are left. – peter.bartos Sep 06 '11 at 23:36
2

Use the attribute fit_center into your ImageView tag.

<ImageView ... android:scaleType="fitCenter" .. />
Roman Black
  • 3,501
  • 1
  • 22
  • 31
Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167
  • Thanks for your fast answer. You mean android:scaleType="fitCenter" right? with that option and both dimensions to fill_parent the result is similar to image 3 (with the image in the middle). And for example with height= "wrap_content" the result is the image 2 with the drawable in the center (horizontally) – Addev Aug 12 '11 at 14:15
  • Use "wrap_content" for both parameters and add android:layout_weight="1" if you are using a LinearLayout – Marcos Vasconcelos Aug 12 '11 at 14:30