2


I'm having trouble getting an image to scale and center properly in an ImageView.
When up-scaling, there are no problems, but when down-scaling, the image no longer appears to be centred, and the bounds of the ImageView are wrong, like this:

problem.png

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:src="@drawable/stereomood_splash"
        android:scaleType="fitCenter"
        android:id="@+id/imageView1"
        android:layout_width="100dip"
        android:layout_height="200dip"></ImageView>
</LinearLayout>

Is this a bug, and has anyone found a workaround?

2 Answers2

6

You should set android:adjustViewBounds="true" as in this answer.

You also need to set either android:layout_width or android:layout_height to be wrap_content, otherwise I believe your hard-coded bounds will override any view bounds adjustments.

Community
  • 1
  • 1
Matthew
  • 44,826
  • 10
  • 98
  • 87
  • Hi, thanks for the response. Please check out my answer also. Apologies - I made a mistake with my example image - it should not have shown the bounds being snapped to the new image size as it did. Incidentally, setting that to 'on' causes the entire image to be invisible in the XML editor. –  Mar 25 '11 at 20:54
2

Edit: Put this here as it's relevant to those encountering the same symptoms.

This problem is only present in the XML editor - on a real phone or emulator the image is sized and positioned correctly. Depending on the particular image and ImageView dimensions, the editor preview will show the image incorrectly resized and/or out of the view bounds.