Touch point coordinates with respect to Image after pinch zoom - android

How to calculate the coordinates with respect to image after zoom the image?
To zoom the image I followed the url: https://github.com/MikeOrtiz/TouchImageView/blob/master/src/com/example/touch/TouchImageView.java.
if we touch the image at particular point before Zoom then corresponding values are
point:(3,2)
top left corner of image:(0,0)
top left corner of screen:(0,0)
scale factors:(1.25,0.98)
After zoom the image:
if we drag the image until the image top left corner coincides the screen top left corner and touch image exactly at same touch point(before pinch) then
point:(540,220)
top left corner of image:(0,0)
top left corner of screen:(0,0)
scale factors:(4.78,2.67)
if we drag the image until the image top right corner coincides with the screen top right corner and touch image exactly at same touch point(before pinch) then
point:(1080,340)
top left corner of screen:(0,0)
top left corner of image:(-2430,0)
scale factors:(4.78,2.67)
if we drag the image until the image bottom left corner coincides with the screen bottom left corner and touch image exactly at same touch point(before pinch) then
point:(670,80)
top left corner of screen:(0,0)
top left corner of image:(0,-890)
scale factors:(4.78,2.67)
if we drag the image until the image bottom right corner coincides with the screen bottom right corner and touch image exactly at same touch point(before pinch) then
point:(456,274)
top left corner of screen:(0,0)
top left corner of image:(-2430,-890)
scale factors:(4.78,2.67)
if we set the image over the screen [ not to set the any corner]
point:(743,146)
top left corner of screen:(0,0)
top left corner of image:(-1280,-423)
scale factors:(4.78,2.67)
In all the above scenarios I am getting the coordinates in touch event as
x_cord=event.getX();
y_cord=event.getY();
The touch points I am getting are with respect to the screen.
How can I calculate the touch points according to the Image?
Thanks & Regards
mini.

We can calculate by calculating relative point according to the image size
float[] values = new float[9];
matrix.getValues(values);
x_coord = ((e.getX() - values[2])*scaleX )/values[0];
y_coord= ((e.gety() - values[5])*scaleY )/ values[4];

In my case I calculated those points as follows. This will be helpful for someone.
x = (event.getX() / values[Matrix.MSCALE_X] - (values[Matrix.MTRANS_X]/values[Matrix.MSCALE_X]));
y = (event.getY() / values[Matrix.MSCALE_Y] - (values[Matrix.MTRANS_Y]/values[Matrix.MSCALE_Y]));

Related

android zooming bitmap based on top left coordinate

I have bitmap with a width of 1182, height of 1822. I want to zoom in with the left top coordinate (765,805) aligning with the left top of the imageview. is this possible?

Set min X, max X, min Y, max Y for a Canvas in Android Studio Kotlin

In Android Studio, I created a canvas. Width = 300dp, Height = 300dp.
I draw a line start from left top corner and end to right bottom corner. I traced the x and y coordinates. It is from (0, 0) in left top corner and to (1000+, 1000+) in right bottom corner.
But,
How to set (0,0) in left top corner and (300,300) in right bottom corner?

Regarding the coordinate of the screen?

I just want to know, isn't the left side coordinate of any UI element is the Y, and the top coordinate is the X, the bottom coordinates is the X and the right coordinates is the Y?
is it the correct coordinate system of the screen, because the (0,0) are placed at the top-left corner of the screen? if so, why oval.set(.., .., .. ..) does not comply to the coorsinates system that starts from the top-left corner (x,y)? in the documentation of, it is states the following:
Parameters
left The X coordinate of the left side of the rectangle
top The Y coordinate of the top of the rectangle
right The X coordinate of the right side of the rectangle
bottom The Y coordinate of the bottom of the rectangle
You probably want to use View.getLocationOnScreen() to get your view coordinates
It will return pix offset values relative to screen's TOP-LEFT point.
Note: getLocationOnScreen() cannot be called before layout buildup is completed.
if in doubt is a ViewTreeObserver to be notified when layout is completed.
Note 2: The above offsets will change once screen orientation changes (landscape vs. portrait).
If important to your app's processing you might want to declare a listener for that event.
See function onConfigurationChanged()

What does top, left, right and bottom mean in Android Rect object

I have an Android project where I should make Apples fall. The apples are painted in a Rect. So I created a function that change the Rect position and repaint. Here's my function :
private void updateApplesPosition() {
for(Rect rect:fallingDownFruitsList)
rect.set(rect.left, rect.top +10, rect.right, rect.bottom +10);
}
I have a problem : the Apples don't fall but go from right to left. To make the apples fall I changed the code by this :
private void updateApplesPosition() {
for(Rect rect:fallingDownFruitsList)
rect.set(rect.left+10, rect.top, rect.right+10, rect.bottom);
}
This image will explain you in detail:
left The X coordinate of the left side of the rectangle
top The Y coordinate of the top of the rectangle
right The X coordinate of the right side of the rectangle
bottom The Y coordinate of the bottom of the rectangle
From the docs
Parameters
left The X coordinate of the left side of the rectangle
top The Y coordinate of the top of the rectangle
right The X coordinate of the right side of the rectangle
bottom The Y coordinate of the bottom of the rectangle
Adding a crucial information.
The documentation says:
Note that the right and bottom coordinates are exclusive.
So if your rectangle is a single pixel at position 10,10
left = 10 : The X coordinate of the left side of the rectangle
top = 10 : The Y coordinate of the top of the rectangle = 10
right = 11 : The X coordinate of the right side of the rectangle plus one
bottom = 11 : The Y coordinate of the bottom of the rectangle plus one
Note that the right and bottom coordinates are exclusive.
The methods getWidth is declared as such
public final int width() { return right - left; }
Here it will return 11 - 10 = 1, as expected.
https://developer.android.com/reference/android/graphics/Rect

Using MotionEvent

From my knowledge x and y starts at the top left of the screen as 0,0
My question is there a calculation to get a positioning in the event? or another way(eg. another new class or method within this class)?
Now the question is vague so I want to add an example:
I remember in math close you have 4 sections in a graph top left, top right, bottom left, and bottom right.
Using the graph style to get variables in:
Getting position from top left to bottom left.
Getting position from bottom right to top right
Getting position from top left to bottom right
Use event.getX() and event.getY() then you can calculate coordinates in any section of graph used in math class.
x, height - y
x, height - y
width - x, height - y

Categories

Resources