Android View review of the coordinate system

Preface

The Android coordinate system is actually a three-dimensional coordinate, the Z axis is up, the X axis is right, and the Y axis is down. The point processing of the three-dimensional coordinates can constitute Android's rich interface or animation effects.

Android relative coordinate system

In Android, the vertex at the top left corner of the screen is taken as the origin of the Android coordinate system. From this point to the right is the positive direction of the X axis, and from this point down is the positive direction of the Y axis. As shown below:
Android absolute coordinate system

View provides methods to obtain coordinates

  • getTop()
    gets the distance from the top edge of the View itself to the top edge of its parent layout
  • getLeft()
    gets the distance from the left side of the View itself to the left side of its parent layout
  • getRight()
    gets the distance from the right side of the View itself to the left side of its parent layout
  • getBottom()
    gets the distance from the bottom edge of the View itself to the top edge of its parent layout
  • getX()
    Gets the coordinates on the X axis of the upper left corner of the View (relative to the parent layout). The return value is getLeft()+getTranslationX(). Note: During the translation of the View, getLeft() represents the X-axis distance of the original upper left corner, and its value will not change. What has changed is getX() and getTranslationX() .
  • getY()
    Gets the coordinates on the Y axis of the upper left corner of the View (relative to the parent layout). The return value is getTop()+getTranslationY(). Note: During the translation of the View, getTop() represents the Y-axis distance of the original upper left corner, and its value will not change. What has changed is getY() and getTranslationY() .

Android absolute coordinate system

Android absolute coordinate system

MotionEvent provides distance related methods

  • getX()
    Get the distance of the touch event from the left side of the control
  • getY()
    Get the distance from the touch event to the top edge of the control
  • getRawX()
    Get the distance of the touch event from the left side of the entire screen
  • getRawY()
    Get the distance of the touch event from the top edge of the entire screen

View width and height related methods

  • The
    return value of getWidth() is mRight-mLeft, which generally refers to the width of the measure, which is valid after calling layout()
  • The
    return value of getHeight() is mBottom-mTop, which generally refers to the height of the measure, which is valid after calling layout()
  • getMeasuredWidth()
    returns the mMeasuredWidth value obtained in the measure process for layout reference
  • getMeasuredHeight()
    returns the mMeasuredHeight value obtained in the measure process for layout reference

Android screen area division

Android screen area
Some coordinates or measurement methods are commonly used in these areas

//获取屏幕区域的宽高等尺寸获取
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int widthPixels = metrics.widthPixels;
int heightPixels = metrics.heightPixels;
//应用程序App区域宽高等尺寸获取
Rect rect = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
//获取状态栏高度
Rect rect= new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
int statusBarHeight = rectangle.top;
//获取状态栏高度
Rect rect= new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
int statusBarHeight = rectangle.top;
//View布局区域宽高等尺寸获取
Rect rect = new Rect();  
getWindow().findViewById(Window.ID_ANDROID_CONTENT).getDrawingRect(rect);  

Special attention: The above methods are best to be transported within or after the onWindowFocusChanged () method of Activity, because only then is the real display.

Reference: A
comprehensive explanation of the Android application coordinate system

Guess you like

Origin blog.csdn.net/xufei5789651/article/details/107596691