简体   繁体   English

如何获得x,y坐标

[英]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. 我的Android应用程序中的一项活动具有一个带有两个imageview的相对布局,这些视图在xml布局文件中设置。 How can I programmatically get x,y location of the imageviews 如何以编程方式获取imageviews的x,y位置

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. 我要做的是让用户触摸image1,然后将其动画化以移动到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. 显然,由于我将这些图像放置在xml中,所以我知道它们的x,y坐标,但是我担心这些坐标会根据屏幕大小和密度而变化。

You can use View.getTop(), View.getBottom(), View.getLeft(), and View.getRight() . 您可以使用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. 这些将返回相对于父视图的top, bottom, left and right边缘的位置。

How to get x and y 如何获得x和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 :) 谢谢 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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