Make Image fit X and scale Y - android

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

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

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.

Controlling button image aspect ratios in xml

Can this be done?
<ImageButton
android:id="#+id/test_button"
android:layout_width="70% of screen width"
android:layout_height="whatever is needed for the image to keep its original aspect ratio"
android:background="#drawable/image" />
You should be able to use something like this:
<ImageButton
android:id="#+id/test_button"
android:layout_width="200dp" // You can use any number here to use a specific size
// You can use dp for density independent pixels
// Or sp for screen independent pixels
android:layout_height="wrap_content" // This will set the height to a size just big
// enough to hold the image.

Categories

Resources