如何获取与屏幕相关的视图坐标?

时间:2014-02-11 03:42:08

标签: android

我有View并且它给了我与其父级的屏幕坐标。 我尝试了getTop()getLeft()方法,但它对我不起作用。 我想从主屏幕坐标?

2 个答案:

答案 0 :(得分:0)

试 使用View.getLocationOnScreen()getLocationInWindow()

例如:

        int loc[] = { 0, 0 };
        View.getLocationOnScreen(loc);
        int x = loc[0]; // x position
        int y = loc[1]; // y position

答案 1 :(得分:0)

试试这个,

 int[] location = new int[2];  // must be array of two integer.

 view.getLocationOnScreen(location);

 xAxis = location[0]; // 

 yAxis = location[1];