CENTER_CROP ImageScaleType scaling ratio - android

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.

Related

Show 2 square pictures above eachother, taking 50% of the width of the screen

I am a webdeveloper, and I am very lost in all the Android screen settings for aligning content. I've seen weight, gravity, scaletypes of the image etc. etc.)
I hope I can make my intentions clear, and that it's possible to fullfill them.
In this picture of a phone screen, I have 2 square pictures (the purple and the green one).
What I want to accomplish is that the width of the pictures always take up 50% of the screen width, and that it is centered (so the margin left and margin right is 25%).
So let's say you have a 600*800 screen, the width should be 300 pixels and the margin left and right would be 150 pixels (or better 25%). On a 768*1024 screen the width should be 384 pixels.
The height should follow that size (I've tried a lot of code, and the best result was that the width was sort of a pertentage, with 3 relative layouts in a lineair layout, but then only the width was correct, but the height wasn't)
Depending on your screen size it could be that the height of the two images together would be higher than the screen and a scrollbar would be neccessary)
Wrap them in PercentRelativeLayout and fix their width 50%. set their height equal to their width in Java when the view gets populated

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.

how to set a imageview height based on its width, calculate it programatically

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.

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

What's the difference between CENTER_INSIDE and FIT_CENTER scale types?

I can't tell the difference between ImageView.ScaleType.CENTER_INSIDE and ImageView.ScaleType.FIT_CENTER.
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).
FIT_CENTER
Compute a scale that will maintain the original src aspect ratio, but will also ensure that src fits entirely inside dst. At least one axis (X or Y) will fit exactly. The result is centered inside dst.
Can someone illuminate the difference between the two?
Here's a graphical illustration of the difference between CENTER_INSIDE and FIT_CENTER.
Image used (100 × 100):
Small image view (75 × 50):
CENTER_INSIDE:
FIT_CENTER:
Both CENTER_INSIDE and FIT_CENTER shrink the image.
Large image view (300 × 200):
CENTER_INSIDE:
FIT_CENTER:
CENTER_INSIDE does not enlarge the image, FIT_CENTER does.
The Android robot is reproduced or modified from work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License.
FIT_CENTER is going to make sure that the source completely fits inside the container, and either the horizontal or vertical axis is going to be exact.
CENTER_INSIDE is going to center the image inside the container, rather than making the edges match exactly.
so if you had a square box that was 10" x 10" and an image that was 8"x8", the CENTER_INSIDE would be directly in the middle of the box with 2" between the source and the destination container.
With the FIT_CENTER, that same image from the example above, would fit the entire container, because the sides are equal, and one axis is going to match the destination. With FIT_CENTER, if you had a box that was 5" x 10", and an image that was 5" x 7", the image would be proportionally scaled, so one of the axis's would fit, but would still center the image inside the destination.
They are similar, but one is made so that the source will fill the destination as much as possible, while the other just centers the image inside the destination.
Hope that clarifies a little
They are the same if the image is bigger than the container.
If the image is smaller then the container CENTER_INSIDE will NOT scale the image up while FIT_CENTER will.

Categories

Resources