Search code examples
androidimageimage-scaling

Inconsistent image scaling on Android for android:scaleType="fitCenter"


I would like to display an image with a border. The solution below works fine for some images, but not for others.

So far my best guess is, that it works fine for smaller images but not for large ones.

With a smaller picture, the solution displays the image nicely with a 2 pixel border. With a large picture the background is too wide in height.

I was testing this with an image that I scaled manually from say 2000 * 400 to 200 * 40. So it was the same image, only prescaled.

Any idea why the large pictures wont scale in the same way as the smaller ones?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:background="#ffffff"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              android:gravity="center" >

    <ImageView
            android:id="@+id/testimage"
            android:background="#000000"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/testimage2"
            android:padding="2dip"
            android:scaleType="fitCenter"
            />
</LinearLayout>

Solution

  • Remove the scaletype and use android:adjustViewBounds="true" for your Imageview.