开发者

ImageView does not center scaled down images properly

I'm having trouble getting an im开发者_StackOverflow社区age 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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜