Android View's view coordinate system

Android coordinate system

Overview

Android has two coordinate systems, namely the Android coordinate system and the view coordinate system.

Screen coordinate system

Insert picture description here

In Android, the upper left corner of the screen is taken as the origin of the Android coordinate system, the origin to the right is the positive direction of the X-axis, and the origin downward is the positive direction of the Y-axis.

View coordinate system

Insert picture description here

View gets its own width and height

method Description
getHeight () Get the height of the View itself
getWidth() Get the width of the View itself

View's own coordinates

method Description
getTop() Get the distance from the top edge of the View to the top edge of its parent layout
getLeft() Get the distance from the left side of the View to the left side of its parent layout
getRight() Get the distance from the right side of the View to the left side of its parent layout
getBottom() Get the distance from the bottom edge of the View to the top edge of its parent layout

MotionEvent method

Coordinate method of touch point

method Description
getX() The distance between the touch point and the left side of the control
getY () The distance between the touch point and the top edge of the control
getRawX() The distance between the touch point and the left side of the screen
getRawY () The distance between the touch point and the top edge of the screen

Accurately obtain the coordinate width and height of the View

Sometimes we need to get Viewthe width and height, but even if we onResumeget it in, sometimes it is not accurate. This is because the measure process of View and the life cycle method of Activity are not synchronized, so onCreate, onStart, and onResume cannot guarantee that the correct information is obtained.

So how do you get the Viewexact width and height?

Activity/View#onWindowFocusChanged()

View has completed the initialization operation, it will be called when the Activity window gets focus and loses focus.

This method will be called multiple times.

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    
    
    super.onWindowFocusChanged(hasFocus);
    if (hasFocus) {
    
    
        int top = imageView.getTop();
        int left = imageView.getLeft();
        int width = imageView.getMeasuredWidth();
        Log.e("TAG", "onWindowFocusChanged: " + top + "-" + left + "-" + width);
    }
}

View#post()

Added to the message queue, the View has completed the initialization operation when the task is executed.

imageView.post(new Runnable() {
    
    
    @Override
    public void run() {
    
    
        int top = imageView.getTop();
        int left = imageView.getLeft();
        int width = imageView.getMeasuredWidth();
        Log.e("TAG", "post: " + top + "-" + left + "-" + width);
    }
});

ViewTreeObserver#addOnGlobalLayoutListener()

Monitor View tree status changes.

imageView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    
    
    @Override
    public void onGlobalLayout() {
    
    
        imageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        int top = imageView.getTop();
        int left = imageView.getLeft();
        int width = imageView.getMeasuredWidth();
        Log.e("TAG", "getViewTreeObserver(): " + top + "-" + left + "-" + width);
    }
});

Guess you like

Origin blog.csdn.net/qq_14876133/article/details/113127313