简书链接:android手机坐标系之getLocationOnScreengetLocationInWindow等总结
文章字数:491,阅读全文大约需要1分钟

1
2
3
4
5
int[] locationScreen = new int[2];
btn.getLocationOnScreen(locationScreen);

int[] locationWindow = new int[2];
btn.getLocationInWindow(locationWindow);

区别:在activity测试暂时没发现区别,如果弹出一个对话框获取对话框的view的话,那么窗口的y x大小肯定小于屏幕大小了。

getY```就是顶部距离了.
1
2
3
4
```view.getTranslateX()```表示相对于原来自身偏移了多少,默认是0 ,如果往右边偏移数值越大,如果往左偏移y就是负数,如果往上偏移就是y负数
触摸事件```getY()```获取当前view相对父亲的顶边界到触摸点的距离```getX()```同理,

```getRawY()```就是获取屏幕到点击触摸点的距离.

btnTousch:action:1,ranx410.54382,ranY:794.4483,x:48.543823

1

view,所在位置:top:591,bottom:687,left362,width:176,362.0,y:591.0,屏幕x:362,,屏幕y:753,窗口x:362,窗口y:753

1
2
3
4
5
6
7
8
模拟点击模拟的是```getRaw```

### bug方面:
触摸滑动计算滑动距离用```getRaw```可以解决抖动问题.
有了这些坐标体系,QQ语音红包就要开始下手了

### 依据

源码得知getRawX就是获取屏幕到view的坐标位置
Returns the original raw X coordinate of this event. For touch
* events on the screen, this is the original location of the event
* on the screen, before it had been adjusted for the containing window
* and views.

### 其他
```getLeft() ```等等就是获取view距离父view的距离了.