In Android imageView wrap_content does not work - android

I've got an ImageView with match_parent width and wrap_content height. When I set an image with a size of 1080x500, the ImageView takes up the entire screen in width, but for some reason takes up extra space in height.I know about AdjustViewbounds, but I'm wondering why imageView takes up extra space in height. I have a Pixel phone (1080x1920).
here is my layout

Check this out https://developer.android.com/reference/android/widget/ImageView.ScaleType.html
CENTER_CROP
Scale the image uniformly (maintain the image’s aspect ratio) so that both dimensions (width and height) of the image will be equal to or larger than the corresponding dimension of the view (minus padding).
CENTER_INSIDE
Scale the image uniformly (maintain the image’s aspect ratio) so that both dimensions (width and height) of the image will be equal to or less than the corresponding dimension of the view (minus padding).
FIT_CENTER
Scale the image using Matrix.ScaleToFit.CENTER
Matrix.ScaleToFit.CENTER: Compute a scale that will maintain the original src aspect ratio, but will also ensure that src fits entirely inside dst. At least one axis (X or Y) will fit exactly. The result is centered inside dst.
FIT_END
Scale the image using Matrix.ScaleToFit.END
Matrix.ScaleToFit.END: Compute a scale that will maintain the original src aspect ratio, but will also ensure that src fits entirely inside dst. At least one axis (X or Y) will fit exactly. END aligns the result to the right and bottom edges of dst.
FIT_START
Scale the image using Matrix.ScaleToFit.START
Matrix.ScaleToFit.START: Compute a scale that will maintain the original src aspect ratio, but will also ensure that src fits entirely inside dst. At least one axis (X or Y) will fit exactly. START aligns the result to the left and top edges of dst.
FIT_XY
Scale the image using Matrix.ScaleToFit.FILL
Matrix.ScaleToFit.FILL: Scale in X and Y independently, so that src matches dst exactly. This may change the aspect ratio of the src.
MATRIX
Scale using the image matrix when drawing.

Related

Difference b/w scaleType:centerCrop and scaleType:centerInside

I've good knowledge of ImageView scaling in Android. But couldn't understand the complete diff b/w scaleTypes: centerCrop and centerInside.
Need some clear explanation.
CENTER_CROP
Added in API level 1
ImageView.ScaleType CENTER_CROP
Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or larger than the corresponding dimension of the view (minus padding). The image is then centered in the view. From XML, use this syntax: android:scaleType="centerCrop".
CENTER_INSIDE
Added in API level 1
ImageView.ScaleType CENTER_INSIDE
Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or less than the corresponding dimension of the view (minus padding). The image is then centered in the view. From XML, use this syntax: android:scaleType="centerInside".
Docs
CenterCrop may crop the image and will always fill the imageview. CenterInside will not crop the image and may leave some part of the imageview not covered by the image.

Keeping ratio of bitmap

I change imageview source with bitmap in code but it does not keep ratio of it. It uses dimensions of the old image.
I set bitmap by;
imageView.setImageBitmap(bitmap);
It stretches bitmap to fit dimensions of old image resource. I want to keep bitmap ratio.
Check out the imageView ScaleTypes: http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
CENTER Center the image in the view, but perform no scaling.
CENTER_CROP Scale the image uniformly (maintain the image's aspect
ratio) so that both dimensions (width and height) of the image will be
equal to or larger than the corresponding dimension of the view (minus
padding).
CENTER_INSIDE Scale the image uniformly (maintain the image's aspect
ratio) so that both dimensions (width and height) of the image will be
equal to or less than the corresponding dimension of the view (minus
padding).
FIT_CENTER Scale the image using CENTER.
FIT_END Scale the image using END.
FIT_START Scale the image using START.
FIT_XY Scale the image using FILL.
MATRIX Scale using the image matrix when drawing.
You want something like CENTER_CROP or CENTER_INSIDE

Get Drawable size of ImageView after scaling

I have an ImageView in my layout. Its width and height are both set to fill_parent. The scaleType is set to fitCenter. I am in landscape mode. So if I have a square src image, when scaled up it will display me two borders, on the left and on the right, which perfectly suits me. But how can I get the dimensions of these borders ? The dimensions of the bitmap won't help for sure, and the dimensions of the ImageView are equal to the screen's.
I have asked for intrinsicwidth but it seems to give me the width of the drawable before it has scaled up.
I have tried adjustViewBounds but the ImageView still fills all the parent.
Any idea ?
Thanks
make a RectF to represent the original picture size, scale it with a matrix into a new RectF, then use this to find your dimensions.
See my answer here Get drawable displayed size after scaling
on how to calculate displayed size for FIT_CENTER scaleType.
And for the calculating the border dimensions just substract calculated value and divide by 2 from the imageView dimesnsions.

Scaling images in an image view with a fixed width

I have several images I lazyload into a ListActivity. Now the thing is that the images have different aspect ratios.
I would like all images to display with the same width set in xml (to fit into my layout), while taking as much space hightwise as they need.
Is there a way to do that in xml?
Thanks!
I think you want android:scaleType="fitCenter"
From the sdk docs at http://developer.android.com/reference/android/graphics/Matrix.ScaleToFit.html#CENTER:
Compute a scale that will maintain the original src aspect ratio, but will also ensure that src fits entirely inside dst. At least one axis (X or Y) will fit exactly. The result is centered inside dst.
So, I think if you set a width on the imageview but set the height to wrap_content you should get what you want with the scaleType fitCenter.

how to scale an image in an ImageView so that it "fits"

I want to scale an image in an ImageView in the following way. The ImageView has some dimensions Width (W) and Height (H). The image I'm putting into the image view could be smaller or bigger than WxH. I want it to scale while preserving aspect ratio to fill WxH space.
It seems like the closest thing to what I want is android:scaleType="centerInside", but what I'm seeing is that if the image is smaller than WxH, it will put a small-unscaled version of that image in the center of the ImageView (like the documentation says), but I want it to scale it to "fit", while showing the entire image, and stretching it to the maximum possible size of the container without cropping anything. In other words, stretch preserving aspect ratio until either the width or the height bumps into the edge of the container (ImageView).
This seems like an obvious thing to want to do, but I can't get it to work this way!!!
From the Android docs...
public static final Matrix.ScaleToFit CENTER
Compute a scale that will maintain the original src aspect ratio, but will also ensure that src fits entirely inside dst. At least one axis (X or Y) will fit exactly. The result is centered inside dst.
The XML attribute for this is...
android:scaleType="fitCenter"

Categories

Resources