Reputation:
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:
<?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?
Upvotes: 2
Views: 3189
Reputation:
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.
Upvotes: 2
Reputation: 44919
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.
Upvotes: 6