Android 获得控件在屏幕中的坐标

Android坐标原点为左上角,如果是某个View,那么就以该矩阵的左上角为原点

1、绝对坐标 Location

int[] location = new  int[2] ;
view.getLocationInWindow(location); //获取在当前窗口内的绝对坐标,含toolBar
view.getLocationOnScreen(location); //获取在整个屏幕内的绝对坐标,含statusBar
// location [0]--->x坐标,location [1]--->y坐标
// 需要在UI控件都加载完成才能正确获取。例如在onWindowFocusChanged(boolean hasFocus)回调中获取。

2、通过LayoutParams 获取位置,需要注意的是,params.y是在窗口内的绝对坐标,不含statusBar

WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.x= x;
lp.y= y;

3、获取相对在它父亲里的坐标

  View.getLeft(), getTop(), getBottom(), getRight()

4、获取屏幕高度、标题高度、状态栏高度详解

5、点击坐标比较详细的文章

原文地址:https://www.cnblogs.com/rison13/p/5526105.html