I find that the unit of the coordinate system of Canvas is different from that of screen.
For example in my case as below:
For one particular point, its on-screen coordinate obtained from ImageView.getX()
and ImageView.getY()
is (336, 578)
.
Then by trial and error, I draw a dot on the Canvas so that this dot falls EXACTLY the same position as the ImageView. I called canvas.drawCircle(330, 440, radius, paint);
to achieve this.
Here comes the question:
Why would the 2 coordinates, (336, 578) and (330, 440), different?
Is it because the screen and the canvas use different units?
Is it an issue regarding pixel, dp and all that?
You could try converting to and from window coordinates using View.getLocationInWindow
this way you'll always be sure you are using the same coordinates system.
Coordinates of ImageView.getX
an Y
are relative to the parent. Canvas coordinates are relative to the view. This could cause the differences
Edit:
I assume in one place you get the coordinates of the view (using getX
and getY
). Convert these to window coordinates. Call these viewXY[]
.
When you need to use them, to draw in your canvas, also convert the top and left coordinates to window coordinates. We call these canvasXY[]
.
Now, the X
position to draw at would be viewXY[0] - canvasXY[0]
, and the Y
position would be viewXY[1] - canvasXY[1]
.