1
votes

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 scaled 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.

3

3 Answers

1
votes

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"
    />
1
votes

You can use

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

if your ImageView is inside a RelativeLayout.

0
votes

Untested, but try adding:

android:layout_gravity="bottom|center_horizontal"

to your ImageView along with FitXY scaling.