Search code examples
androidimageviewscaleandroid-view

Center drawable and scale height


Since I want to support tablets and smartphones without delivering a special "HD" version, I need to scale my Images depending on the resolution of the users device. This is the ImageView source that looks good on the Samsung Galaxy Nexus:

  <ImageView
                        android:id="@+id/character"
                        android:layout_width="wrap_content" 
                        android:layout_height="wrap_content"
                        android:layout_alignParentBottom="true"
                        android:layout_centerHorizontal="true"
                        android:background="@android:color/transparent"
                      android:scaleType="centerCrop"
                       android:src="@drawable/ma_un1_001" />

enter image description here

But it will cut the image on the nexus 7. enter image description here

When I changed the width and height to:

<ImageView
                android:id="@+id/character"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_alignParentBottom="true"
                android:background="@android:color/transparent"
                android:scaleType="centerCrop"
                android:src="@drawable/ma_un1_001" />

Unfortunately this scales the image out of the view: enter image description here

changing the xml to

android:layout_width="wrap_content"
android:layout_height="fill_parent"

Would still cut the image.

So I changed the scaleType to FIT_START

<ImageView
                android:id="@+id/character"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_alignParentBottom="true"
                android:background="@android:color/transparent"
                android:scaleType="fitStart"
                android:src="@drawable/ma_un1_001" />

enter image description here

Looks great on all my devices, but the drawable is aligned to the left. Is there any way to center it?


Solution

  • Thanks to Wenhui I found the solution.

     <ImageView
                    android:id="@+id/character"
                    **android:layout_width="fill_parent"
                    android:layout_height="fill_parent"**
                    android:layout_alignParentBottom="true"
                    android:background="@android:color/transparent"
                    **android:scaleType="fitCenter"**
                    android:src="@drawable/ma_un1_001" />
    

    Scales the Image to every screensize and won't cut anything. Thank you very much.