当前位置: 移动技术网 > IT编程>移动开发>Android > Android 坐标系与视图坐标系图解分析

Android 坐标系与视图坐标系图解分析

2019年07月24日  | 移动技术网IT编程  | 我要评论

中国军事武器图片,rosy dilettuso,laclover

1. android坐标系

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

从原点向右是x轴的正方向,从原点向下是y轴的正方向

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

该方法的具体实现

/**
   * <p>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.</p>
   *
   * @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():获取点击位置离屏幕顶边的距离

以上这篇android 坐标系与视图坐标系图解分析就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网