Center drawable and scale height

2019-06-08 03:00发布

问题:

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" />

But it will cut the image on the nexus 7.

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:

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" />

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

回答1:

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.