Android: ImageView, keep the aspect ratio - android

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

Related

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.

CENTER_CROP ImageScaleType scaling ratio

Here is what the official documentation for center_crop is:
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).
What i don't understand is how saying "equal to or larger than the corresponding dimension of the view(minus padding)" is justified. For a image of arbitrary dimensions, how can i predict what the rendered image will look like?
So, given a image, if they say scaling will be such that dimansions will be equal to or larger than, how can i say what ratio will be used to scale the image. Can someone tell me just how 'larger than' can be predicted?
Does the scaling ratio depend upon the size of the image??
The image will be scaled PROPORTIONALLY to fill the whole place which you set with height and width. When both height and width will be reached, the parts of the image which are larger than a corresponding size (the height or the width), will be cut off. So, if the width will be larger, then the image will be cut off from the left and the right sides. The image will be cut off only in one direction - only horizontally, or only vertically.

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.

Imageview scaling keeping height fix and width variable

I want to make height of a imageview constant and based on aspect ratio width should vary. But I can see if I give width as wrap_content then height is less than fixed height because keeping aspect ratio, height corresponding to wrap_content width is less fixed height. I want width to adjust keeping height exact a fix value.
You can change the scaleType to MATRIX. And then you can manage the size by your self, it will not try to save aspect ratio
https://github.com/KyoSherlock/AspectRatioLayout
May be this is what you want. You can use AspectRatioImageView.
<com.sherlock.aspectratio.AspectRatioImageView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:background="#android:color/holo_blue_bright"
android:contentDescription="#null"
android:scaleType="centerCrop"
android:src="#drawable/ic_launcher"
app:widthHeightRatio="2" >

CENTER_CROP does not maintain image ratio

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.

Categories

Resources