I still don't know exactly why getX()
, getY()
, setX()
, and setY()
do not represent the position of the View on the screen, but I figured out how to get the actual location of the view objects on the screen: getLocationOnScreen(coordinates)
. Where coordinates
is a 2 position int array (int[2]).
For example, to get the real value correspondent to the top-left corner of my ImageView image
I simply need to do something like:
int[] img_coordinates = new int[2];
ImageView image = (ImageView) findViewById(R.id.myImage);
image.getLocationOnScreen(img_coordinates);
And then I can actually compare these coordinates with the Touch-event's getRawX
and getRawY
.
If I want to get the center point of my view I just need to do this:
x_center = (double)img_coordinates[0] + image.getWidth()/2.0;
y_center = (double)img_coordinates[1] + image.getHeight()/2.0;
I hope this helps other people too, it took me a while to find this approach and I am actually not sure if it is the best approach but it does the trick for me.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…