CENTER_CROP does not maintain image ratio - android

By reading the Android doc, I expect a picture with this layout:
<ImageView
android:id="#+id/avatar"
android:layout_width="fill_parent"
android:layout_height="150dip"
android:adjustViewBounds="true"
android:scaleType="centerCrop" />
to fill the layout (height of 150 dip is the only condition) without any distorsion:
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).
That works pretty well on phones:
But on tablet the image is totally distorted:
How is that possible to say: "please do not stretch horizontally"

I think you're specifying the image as a background rather than as a source. The images look stretched on the phone as well, and CenterCrop should work fine.
Try using the setImageBitmap or the "src" attribute and let me know if it works.

Related

Trying to get an ImageView to scale as the screen gets bigger

I'm currently new to Android development, and I'm was trying to get an image to scale for different screen sizes. I made a drawable-hdpi,-mdpi, and drawable-xdpi. In these folders I placed the image but at different sizes for each screen density ( Ex. for xdpi, I made the image bigger). I placed the imageView in the constraint layout and set the height and weight to wrap_content. I was expecting the image to be the same size as the corresponding drawable, but for higher screen densities it was still super small. Any thoughts? I also read about 9 patch images, but I cant just add a larger image to the higher density drawable folders ?
you should change the scaleType of imageview.
if you want to scale image with maintain aspect ratio you should use
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitcenter"/>
u can try another option like centerCrop and fitxy and see the result
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
....
Add android:scaleType="fitXY" to ImageView to adjust the width and Height of view

Android: Is there a way to have an image fill the screen without stretching or cropping it

I'm using android:scaleType="centercrop" or android:scaleType="center"
It more or less ruins the image
I'm using Android Studio if that makes any difference.
It sounds like you want centerInside, which has the following behavior --
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).
For more details, see the ScaleType docs.

Android: ImageView, keep the aspect ratio

Lets say I have image test.png with size h:40px w:80px
And I have ImageView, with height: 80px.
How to set image to this ImageView, so that image could fit ImageView but proportionally ?
So I need image to fit height, that mean it should become 80px height,
and fit width as much as needed for image to be proportional, so width should become 160px
I know I can just cut a bigger image, its not a solution for me.
Tried:
android:adjustViewBounds="true"
android:scaleType="fitXY"
but its not adjust width.
You have an ImageView (or other View) that you want to have a width of fill_parent and a height scaled proportionately:
Add these two attributes to your ImageView:
android:adjustViewBounds="true"
android:scaleType="centerCrop"
And set the ImageView width to fill_parent and height to wrap_content.
See android:adjustViewBounds.
fitXY does not keep the aspect radio. You have to use one from CENTER_CROP and CENTER_INSIDE. From the doc
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
Use the ‘android:src’ instead of background

Why does setImageBitmap override layout_height and layout_width?

For the following layout:
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myid"
android:layout_width="200dp"
android:layout_height="200dp"
/>
If i have a 400x400dp bitmap, why does calling setImageBitmap with it override the width and height? In other words: why does does an imageview with width and height set to 200 show up as 400x400 instead of automatically scaling the image?
I know that there is an option to manually rescale the bitmap, but it seems odd that if I already specified the dimensions of my imageview in the layout, I still have to rescale the image.
use ImageView.ScaleType FIT_XY or CENTER_CROP if you want to mantain the ratio
android:scaleType="centerCrop"
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).
android:scaleType="fitXY"
Scale in X and Y independently, so that src matches dst exactly. This may change the aspect ratio of the src.

Make Image fit X and scale Y

I have this ImageView
<ImageView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/offer_img" />
I'm loading images into this ImageView from a URL and I want the image to always fit X (width of the screen) and readjust the height to keep the aspect ratio. Do I need some Java to do this or can I do it through XML alone?
Try this:
<ImageView
android:scaleType="centerInside"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/offer_img" />
You will obtain what you expect only if original image ratio is around the 8:5 ratio
You could try it with android:scaleType
You could use the method that is the answer in this question: Java image resize, maintain aspect ratio

Categories

Resources