1.Android坐标系

在Android中,将屏幕的最左上角顶点作为Android坐标系的原点

从原点向右是X轴的正方向,从原点向下是Y轴的正方向

View提供了getLocationOnScreen( int[] location)方法来获取在整个屏幕内的绝对坐标,该坐标值为View左上角的坐标。注意该View的坐标值是从屏幕左上角开始获取的,所以也包括了通知栏的高度
该方法的具体实现

    /**     * 

Computes the coordinates of this view on the screen. The argument * must be an array of two integers. After the method returns, the array * contains the x and y location in that order.

* * @param location an array of two integers in which to hold the coordinates */
public void getLocationOnScreen(@Size(2) int[] location) { getLocationInWindow(location); final AttachInfo info = mAttachInfo; if (info != null) { location[0] += info.mWindowLeft; location[1] += info.mWindowTop; } }

可看到,传入的int[]数组中,location[0]代表的是X轴坐标,location[1]代表的Y轴坐标
这里还有个getLocationInWindow方法,作用是获取View在当前窗口内的绝对坐标
我们在通过MotionEvent类中的getRawX(),getRawY()方法获取的坐标同样也属于这种Android坐标系里的坐标

2.视图坐标系

Android中的视图坐标系,描述的是子视图与其父视图中的位置关系
和Android坐标系一样,视图坐标系也是以原点向右为X轴正方向,以原点向下为Y轴正方向,与Android坐标系不同的是,视图坐标系的原点是以父视图左上角的位置为原点

如上图中,对于Button来说,父视图LinearLayout左上角就是视图坐标系的原点(0,0)
我们通过MotionEvent类中的getX()、getY()方法所获得的就是视图坐标系的坐标

在Android中,系统提供了很多获取坐标值、相对距离等方法

View提供的API

  • getTop():获取View顶边到其父布局顶边的距离
  • getLeft():获取View左边到其父布局左边的距离
  • getRight():获取View右边到其父布局左边的距离
  • getBottom():获取View底边到其父布局顶边的距离

MotionEvent提供的API

  • getX():获取点击位置离View左边的距离
  • getY():获取点击位置离View顶边的距离
  • getRawX():获取点击位置离屏幕左边的距离
  • getRawY():获取点击位置离屏幕顶边的距离

更多相关文章

  1. Android坐标的简单方法介绍
  2. Android仿百度外卖自定义下拉刷新效果
  3. android 坐标相关
  4. Android(安卓)吸入动画效果实现分解
  5. Android系列教程之三:Android项目的目录结构
  6. Android(安卓)游戏引擎libgdx 自适应分辨率
  7. Android游戏开发基本知识
  8. 框架模式MVC 在Android中的使用
  9. 自定义View最详细的资料整理与总结

随机推荐

  1. 解决Android模拟器不能联网问题
  2. Android搜索框输入内容点击键盘的搜索按
  3. Android(安卓)小项目之---Toast对象详细
  4. 【react-natvie】react-native Android(
  5. Android相机实时自动对焦的完美实现
  6. Android基于环信SDK开发IM即时聊天(一)
  7. 安全与权限
  8. android的 网络下载和数据持久化
  9. 中国市场 Android(安卓)App 兼容性报告
  10. 在QQ通讯录之前拦截短信