开发者

Android image scaling and alignment

I'm trying to show an image scaled while preserving its aspect ratio, but at the same time align it to the bottom.

Using android:scaleType="FitXY" causes the image to be centered vertically and horizontally, so it doesn't get aligned to the bottom. Using "FitEnd" causes the image to be aligned to the bottom right corner, so it isn't centered horizontally.

Is there any way to get around this? Maybe using some matrix to scale it (scaleType="matrix")?

EDIT: To clarify a bit on what it is I want exactly...

I have an ImageView, whose layout (location, size, gravity, etc.) I can't change. I want to load a bitmap as its source image, but have that bitmap get sc开发者_开发技巧aled to the ImageView's size (preserving the aspect ratio of the bitmap) and then aligned to the ImageView's bottom.

EDIT: After trying everything I could think of (and everything that was suggested here), we ended up sub-classing ImageView and doing the scaling/translation ourselves in onDraw.


Try using android:scaleType="centerInside" instead of fitXY.


Layout being used in a Blrfl Labs application to scale an image to fit the area the layout gives it without hosing up the aspect ratio:

    <ImageView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1.0"
            android:gravity="center"
            android:src="@drawable/blah_blah_blah"
            android:scaleType="centerInside"
            android:adjustViewBounds="false"
    />


You can use

android:layout_centerHorizontal="true"
android:scaleType="fitEnd"

if your ImageView is inside a RelativeLayout.


Untested, but try adding:

android:layout_gravity="bottom|center_horizontal"

to your ImageView along with FitXY scaling.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜