简体   繁体   中英

How to get x,y coordinates

One of the activities in my Android app has a relative layout with two imageviews, which are set up in an xml layout file. How can I programmatically get x,y location of the imageviews

Will these coordinates be different dependent on screen size and density?

What I want to do is have the user touch image1 which will then be animated to move to the location of image2.

Obviously, since I placed these images in the xml I know their x,y coordinates, but I'm concerned that these coordinates will vary dependent on the screen size and density.

You can use View.getTop(), View.getBottom(), View.getLeft(), and View.getRight() . These will return the location of the top, bottom, left and right edge of the View relative to the parent.

How to get x and y

int[] t = new int[2];
int[] cordinatesofImageView1 = imageView1.getLocationOnScreen(t);


int[] cordinatesofImageView2 = imageView2.getLocationOnScreen(t);

You have to adjust your control wrt of screen size and Here is the link that how can you detect that.

Determine device screen category (small, normal, large, xlarge) using code

OR

You can refer to this answer this may help you.

Interchange location of two views

Thanks :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM