Android screen, coordinate system, Padding, Margin

1. Screen

insert image description here

2. Classification of coordinate systems

1. Screen coordinate system

 insert image description here

The coordinate system takes the upper left corner of the screen as the origin (0, 0), the horizontal right direction represents the positive direction of the x direction, and the vertical downward direction represents the positive direction of the y direction. In touch events, the getRawX() and getRawY() methods are used. The obtained coordinates are the coordinate values ​​based on this coordinate system.

2. Layout coordinate system

The coordinate system takes the upper left corner of the viewgroup as the coordinate origin, the horizontal right represents the positive direction of the x direction, and the vertical downward represents the positive direction of the y direction. The coordinates of the view are determined during the layout layout process. That is, the position of the view in the viewgroup

3. View coordinate system (relative to the parent container)

The view coordinate system takes the upper left corner of the parent view as the coordinate origin, and it describes the position of the child view in the parent view.
The corresponding origin is to the right for the positive direction of the x-axis, and the origin is downward for the positive direction of the y-axis. In touch, the coordinate value obtained by getX() and getY() is the coordinate value in the view coordinate system.

insert image description here

 

3. Get the coordinates

There are three black boxes in the picture, the outermost layer is the mobile phone screen, the middle layer is the ViewGroup, and the innermost layer is the view placed in the ViewGroup.

insert image description here

1. The obtained coordinates and distance methods provided by View (relative to the coordinates of the parent container)

View.getTop()            获取到的是view自身的顶边到其父布局顶边的距离
View.getLeft()           获取到的是view自身的左边到其父布局左边的距离
View.getRight()          获取到的是view自身的右边到其父布局左边的距离
View.getBottom()         获取到的是view自身底边到其父布局顶边的距离
这四个方法获取的坐标表示的是View原始状态时相对于父容器的坐标,对View进行平移操作并不会改变着四个方法的返回值。

View.getX()              获取的是View左上角相对于父容器的坐标X
View.getY()              获取的是View左上角相对于父容器的坐标Y
View.getTranslationX()   View左上角相对于父容器的X偏移量 translationX = getX() - getLeft()  当View未发生平移操作时,translationX 与translationY都为0。
View.getTranslationY()   View左上角相对于父容器的Y偏移量
View.getLocationOnScreen(int[] position) 获取View相对于整个屏幕的坐标
View.getLocationInWindow(int[] position) 获取View相对于Window的坐标(忽略状态栏及ActionBar)

view

Added some attributes about view (x, y): indicate the coordinates of the upper left corner of the view, its values ​​are: x and y default to 0
x = mleft + tranlationX
y = mtop + tranlationY
translationX, translationY: Indicates the offset of the view position The amount of displacement (relative to the original position), the initial value is 0.
This coordinate system is mainly used for the animation operation of the view, which can control the offset of the entire content of the view

2. The method provided by MotionEvent (to get the coordinates of the user's click point)

getX()       获取点击事件距离当前View左边的距离,即视图坐标
getY()       获取点击事件距离当前View顶边的距离,即视图坐标
getRawX()    获取到的是点击事件距离整个屏幕左边的距离,即绝对坐标
getRawY()    获取到的是点击事件距离整个屏幕顶边的距离,即绝对坐标

insert image description here

4. Padding and Margin

getPaddingLeft():  View里的content距离View左边缘的距离
getPaddingTop():   View里的content距离View上边缘的距离
getPaddingRight(): View里的content距离View右边缘的距离
getPaddingBottom():View里的content距离View下边缘的距离

Guess you like

Origin blog.csdn.net/ahou2468/article/details/123230038