Android scaleType aspectFill - android

What is the value for android:scaleType that will achieve the effect of scale aspect fill? In other words, I need the image to keep it's aspect ratio, but it should fill the canvas. This will obviously lead to part of the image being cut off (if canvas and image aren't similar rectangles) but I want it this way.

Well, you could look at the documentation. CENTER_CROP seems to be what you're looking for:
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 value you appear to want is 'CENTER_CROP'.

Center_crop is what you're searching for. This property will expand the image until fill the container, preserving in center and ratio.

<ImageView
android:layout_width="100dp"
android:layout_height="400dp"
android:src="#drawable/ic_launcher"
android:scaleType="centerCrop"/>

Related

adjustViewBounds gives wrong width - Android

I am trying to display an image which will have the width of the parent view and the height will be calculated to keep the aspect ratio.
The problem is that the width of the image expands beyond parent view width, like the image is zoomed.
<com.facebook.drawee.view.SimpleDraweeView
android:id="#+id/imageview_mainimage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
/>
How can I fix this?
This happens because your SimpleDraweeView doesn't have the same aspect ratio as your loaded image, and adding android:scaleType="centerCrop"will make the SimpleDraweeView try to load the content cropping the image to best fit the smallest of the 2 dimensions. You can either expect the image to have a certain ratio fresco:viewAspectRatio="1.33", or use a different option for scaleType

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.

Image not stretching inside imageView android

I am having a layout in with root Relative layout and and ImageView inside it when i set it source the Image is not fitting inside into Image view leaving Top and and bottom padding which is in preview but not defined in xml. I want to know how to stretch image to fit completely in ImageView.
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff">
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/place_demo_2"
android:id="#+id/back_ground_place"
android:layout_alignParentBottom="true"
android:layout_marginBottom="400dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Manila Main"
android:id="#+id/visit_place_title"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="68dp"
android:textColor="#fff"/>
</RelativeLayout>
Using Code:
YourImageView.SetScaleType(ImageView.ScaleType.FIT_XY);
Using xml:
android:scaleType="fitXY"
See also below snapshots for other alternative options.
Use android:scaleType in your ImageView
android:scaleType="fitXY"
Check other available options for scaleType here
You're going to want to use android:scaleType in your XML file. There are several options you will probably want to play with until you get the exact product you are looking for but, if you don't care about aspect ratio, it sounds like you might want to use android:scaleType="fitXY".
You can find more information here:
https://developer.android.com/reference/android/widget/ImageView.ScaleType.html
Below are the rest of the options:
ImageView.ScaleType="CENTER"
Center the image in the view, but perform no scaling.
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).
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).
ImageView.ScaleType="FIT_CENTER"
Scale the image using CENTER.
ImageView.ScaleType="FIT_END"
Scale the image using END.
ImageView.ScaleType="FIT_START"
Scale the image using START.
ImageView.ScaleType="FIT_XY"
Scale the image using FILL.
ImageView.ScaleType="MATRIX"
Scale using the image matrix when drawing.
either make width of Imageview to wrap_content
or
you have to set android:scaleType of Imageview to fitXY
Add to ImageView attr android:scaleType="fitXY"
That's because you have aligned the ImageView to parent's bottom BUT there is a margin at the bottom of 400dp.
Long story short, remove
android:layout_alignParentBottom="true"
android:layout_marginBottom="400dp"
Also set the layout_height to match_parent if you want the image to fill the whole screen.
P. S. Also fill_parent is deprecated, so use match_parent instead.

Android ImageView get scaled image on screen

In my code I'm loading image from source into my ImageView. ScaleType of this image view is FIT_CENTER.
My original image is far bigger than on screen. Is it possible to obtain width and height or scale ratio of image on screen?
I can do it manually like based on orientation of image, calculate ratio of area that ImageView is in and ratio of image itself. After ratio I can assume which dimension is fitted (like width or height) and so on...
Is there any other easiest way to do it?
You can scale your image retaining aspect ratio by setting the adjustViewBound to true and scaleType to fitXY.
android:adjustViewBounds="true"
android:scaleType="fitXY"
Use ImageView's
getDrawable().getIntrinsicHeight()
Taken from this related question.
hi set your scale type fitXY
like as this. android:scaleType="fitXY"
There is another way to "auto-scale" your image. You should set the loaded bitmap as background of ImageView control instead of set as source.
Hope this helps.

How do I display this image in Android?

How do I display an image with the following configuration in Android?
Retain original aspect ratio (the image can be scaled, but not stretched)
Fill the width of the parent
Aligned to the bottom of the parent
The following does not work:
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:layout_alignParentBottom="true"
android:src="#drawable/background" />
Using the above xml the ImageView takes the width of the parent, but the image inside the ImageView does not.
Some points.
If you want to manteint aspect ratio instead of fitCenter you should try centerInside:
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).
Remember that devices using your application might have different screen sizes.
If your background image is something that can be stretched I would recommend you try a NinePatchDrawable

Categories

Resources