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" >
Related
for example:
I am having a imageView width fixed of 600dp and height has to be calculated based on the given image height and fixed width(600dp). it should be in such a way that it matches the width not much big not small.
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
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.
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
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.