Locate the same area of an ImageView regardless of scaling - android

I want to identify a certain part of an ImageView -- based on the actual size of the image regardless of how the image is scaled for the particular device.
Example: I have an image that is 480x320. I want a rectangle that (at this size) is defined as
Rect(10,35,20,50). What I'm wanting to do is figure out how I can define this rectangle relative to the actual size of the image when it scaled and displayed on the screen.

Can you just scale each part of the rectangle? For instance, if your image is displayed at 2x -- 960x480 -- then would the correct rectangle be (20,70,40,100)?

Use percentages from a pivot point: the top-left or the bottom-right.

Related

ImageView android:cropToPadding, what it actually does?

I went through the documentation for the tag android:cropToPadding here, it only says:
If true, the image will be cropped to fit within its padding.
May be a boolean value, such as "true" or "false".
which is quite confusing for me to understand.
I have an ImageView inside my app, (which was developed by someone else):
<ImageView
android:layout_width="125dp"
android:layout_height="125dp"
android:layout_gravity="center"
android:maxWidth="100dp"
android:padding="20dp" />
This ImageView had cropToPadding tag inside it, there were like 20 ImageView on main screen, which all had this tag inside them, and the app was obviously taking time to load as there were more Images, but then removing images was not an option, so I was finding stuff that was useless and trying to optimize the layout when I came across this tag.
Removing this tag did no change to the images that were shown inside the ImageView, but there must be some reason that every image contained this tag. So I started finding what this tag did, and documentation wasn't much clear as to why this tag should be used.
Can someone please explain what this tag does to the Image? I found out not many resources, all that I found was "This crops the Image to padding", what does that mean! I know what padding is, I know what cropping is, but what does "Sets whether this ImageView will crop to padding" mean?
This is a complex question to answer, because we have to drill into some nitty-gritty details of how ImageView actually draws the image to the screen.
The first thing to establish is that there are two rectangles that affect ImageView drawing behavior. The first is the rectangle defined by the ImageView's dimensions ignoring padding. The second is the rectangle defined by the ImageView's dimensions considering padding. (Obviously, if padding is 0, then these will be the same.)
The next thing to establish is that ImageViews all have a scale type that defines how the image is stretched and/or cropped when the image's intrinsic size doesn't match the size of the rectangle that it is being drawn into.
The default scale type is FIT_CENTER, which scales the image down to fit within the view bounds + padding (that is, the image will be drawn inside the rectangle that considers padding). Since the image is being drawn inside the padding rectangle, android:cropToPadding has no effect.
However, other scale types work differently. The scale type CENTER simply positions the image in the middle of the view, but performs no scaling (so the image will be clipped if it is bigger than the view). In this case, android:cropToPadding defines whether the image will be clipped by only the view's bounds or also clipped by the view's padding.
A picture is worth a thousand words:
This picture shows the same 72x72 image inside a 72x72 view with 16dp padding and CENTER scale type. The left ImageView has android:cropToPadding="false" and the right ImageView has android:cropToPadding="true".

Is image cropping possible in an Android app?

Is it possible to crop an image on Android?
For example:
If the image must fit in a 400w x 400h space and it is 600w x 400h, can you horizontally center align it within the 400w x 400h placeholder space and crop out 100 pixels on the left and 100 pixels on the right.
OR
If the image must fit in a 400w x 400h space and it is 400w x 600h, can you vertically center align it to the image placeholder and crop out 100 pixels on the top and 100 pixels on the bottom.
This may also apply to images of many different resolutions coming in from a server - regardless of the resolution of the image, can you accomplish the above?
I found this but this is specifically for users to be able to crop their images manually in the app (https://medium.com/mindorks/android-top-image-cropper-libraries-3bc4a4f8f2df), but I need to have the image pull in to a set placeholder size in the app from the API.
Thanks!

ImageView coordinate (0,0) has space

Im facing the next problem: I have an imageView that change the size programmatically because de density hdpi has a 480x800 resolution. I change the size with layoutParams.
After I change the size, I want to position the imageView. When I position the view for example to the coordinate (0,0) I saw that between the imageView and the limit of the screen is a little space, like 25 inches. The imageView moves to other place and when I want to position it again to (0,0), the space between the imageView and the limit of the screen disappear. Why is happening that? I need to fix that because the first time is showing wrong, I don’t want the space!
Hope your understand
Greets

Using setLayerInset on LayerDrawable with Bitmap gives unexpected results

I'm dynamically placing small dot bitmaps ('hotspots') over a large bitmap image using LayerDrawable. The layer at index 0 contains the large image. The dots' positions on the image are specified to me in terms of percentage of large image. For example, I might have a hotspot that is supposed to be 50% from the left and 75% from the top of the large base image. In order to position the dots over the correct part of the image, I'm using
setLayerInset(layer, leftOffset, topOffset, rightOffset, bottomOffset)
and calculating the offsets based on the width and height of the ImageView which contains the LayerDrawable. I'm happy that the calculations are correct and that the right height and width are being retrieved, and expect the dots to be correctly displayed, but they are not. The dots are being skewed along the Y axis and their positions are incorrect (too high and too far to the left). Has anyone encountered a similar problem and found a solution?
I spend a day and a half working on this, trying to make sense of the numbers, and here's what I found:
In the situation I am dealing with, where the first layer contains a BitmapDrawable and I'm setting insets for the subseqent layers, the offset in the call:
setLayerInset(layer, leftOffset, topOffset, rightOffset, bottomOffset)
are relative to the dimensions of the bitmap in the first layer, even if those dimensions are greater than the view in which the DrawableLayer is being placed (e.g. when the Bitmap is being redimensioned automatically by the view).
Bizarre and unexpected, but true. Hope this saves somebody some time.

how to scale an image in an ImageView so that it "fits"

I want to scale an image in an ImageView in the following way. The ImageView has some dimensions Width (W) and Height (H). The image I'm putting into the image view could be smaller or bigger than WxH. I want it to scale while preserving aspect ratio to fill WxH space.
It seems like the closest thing to what I want is android:scaleType="centerInside", but what I'm seeing is that if the image is smaller than WxH, it will put a small-unscaled version of that image in the center of the ImageView (like the documentation says), but I want it to scale it to "fit", while showing the entire image, and stretching it to the maximum possible size of the container without cropping anything. In other words, stretch preserving aspect ratio until either the width or the height bumps into the edge of the container (ImageView).
This seems like an obvious thing to want to do, but I can't get it to work this way!!!
From the Android docs...
public static final Matrix.ScaleToFit 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.
The XML attribute for this is...
android:scaleType="fitCenter"

Categories

Resources