views:

790

answers:

2

I have an ImageView with max height and max width both set to 100. The figure below is clearly not a square, but you can use your imagination ;)

Figure 1:

╔══════════════════════════════════════════════╗
║ ImageView    ╔══════════════╗                ║
║              ║              ║                ║
║              ║ Actual image ║                ║
║              ║              ║                ║
║              ║              ║                ║
║              ║              ║                ║
║              ╚══════════════╝                ║
╚══════════════════════════════════════════════╝

Anyway, If I try to set a BitMap to the ImageView that does not have a ratio of 1:1, the image is positioned like pictured in Figure 1. What I want is for the picture to be placed to the left inside the ImageView like pictured in Figure 2 below.

Figure 2:

╔══════════════════════════════════════════════╗
║══════════════╗                               ║
║              ║                               ║
║ Actual image ║                               ║
║              ║                               ║
║              ║                               ║
║              ║                               ║
║══════════════╝                               ║
╚══════════════════════════════════════════════╝

You can see my ImageView in XML below. maxHeight, maxWidth and adjustViewBounds are set during runtime.

<ImageView android:id="@+id/someImage"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textName"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:paddingRight="4dp"
    />

This is in a RelativeLayout if it makes any difference.

A: 

Try

android:gravity="left"

Gravity is how a view positions its contents. Works on TextView for text and probably ImageView for image.

Edit:

Try making a FrameLayout that contains the ImageView. Set the ImageView width and height to "wrap_content" and set the gravity of the parent to "left".

Otherwise, implement onDraw in a View and draw the image at whatever position you want.

drawnonward
A: 

I use:

android:scaleType="fitCenter"

documented here. Good luck!

EDIT: My bad, I didn't realize you were centering an image in an ImageView (my first reading I thought you were having trouble centering the ImageView within another View). Now I'm not so sure, but you could look at ScaleType anyways ^_-

paul.meier