I have an ImageView
with max height and max width both set to 100
. The figure below is clearly not 开发者_Python百科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.
try this
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/home_icon"
android:scaleType="fitStart"/>
It just helps for me.
By default, the scaleType is "fitCenter" / ScaleType.FIT_CENTER like in your first sketch.
To align the drawable to the left of the ImageView, as shown in your second sketch:
in the xml view:
<ImageView
android:scaleType="fitStart" />
or in the Java class:
ImageView image_view = [create new ImageView or getView from xml];
image_view.setScale(ScaleType.FIT_START);
To align the drawable to the right of the ImageView
in the xml view:
<ImageView
android:scaleType="fitEnd" />
or in the Java class:
ImageView image_view = [create new ImageView or getView from xml];
image_view.setScale(ScaleType.FIT_END);
Why don't you just add a drawableLeft to your View.
Like this (in your layout.xml): android:drawableLeft="@drawable/yourDrawableId" -or- like this (in Java code): yourView.setCompoundDrawablesWithIntrinsicBounds(yourDrawableId,0, 0,0);
You can control the padding, etc of this drawable inside your View container (to control close grained layout tweaks).
I faced the same problem and this is how I solved it:
I ended up putting the ImageView inside a RelativeLayout that is just as big as the ImageView and set the gravity of the Relative Layout to what I wanted (left in your case).
I also set adjustViewBounds to true so the ImageView is just as big as the Image and is positioned inside the Relative Layout.
Not a very elegant solution but it works...
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 ^_-
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.
Something is not right. Your ImageView is defined with wrap_content
for both its layout_width
and layout_height
in the XML, so it should always only occupy the same space as your bitmap.
Try adding a android:background
for your ImageView to see that it really is wider than your bitmap and caused the unwanted centering:
<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"
android:background="#F00"
/>
Test it with some bitmaps that has transparent background color so we can see the red background from the ImageView. I expect it to show a tall rectangle if your source bitmap is tall and a wide rectangle if the bitmap is wide. You will only see a square if your source bitmap is square. We should never see the Figure 1 case from your question.
Maybe you wanted to have your ImageView to always have a 100x100 dimension at all times instead? Then you can use the following code (notice the fitStart
for the scaleType
attribute as correctly mentioned by others already):
<ImageView android:id="@+id/someImage"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textName"
android:layout_width="100dp"
android:layout_height="100dp"
android:paddingRight="4dp"
android:background="#F00"
android:scaleType="fitStart"
/>
There is no need to set maxHeight
, maxWidth
and adjustViewBounds
from the Java code if this is what you want.
精彩评论