A comprehensive explanation of the Android advanced view coordinate system

A comprehensive explanation of the Android advanced view coordinate system

Preface

First of all, I wish you all a happy National Day and have fun;

1. Detailed explanation of Android coordinate system

The Android coordinate system is actually a three-dimensional coordinate system, with the Z axis pointing upward, the X axis pointing right, and the Y axis pointing downward. The point processing of these three-dimensional coordinates can form Android's rich interface or animation effects;

1. Obtaining some values ​​of the Android screen

① Get the width and height of the screen area

  1. DisplayMetrics metrics = new DisplayMetrics();
  2. getWindowManager().getDefaultDisplay().getMetrics(metrics);
  3. int widthPixels = metrics.widthPixels;
  4. int heightPixels = metrics.heightPixels;

②Get the width and height of the application area

  1. Rect rect = new Rect();
  2. getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);

③Get the status bar height

  1. Rect rect = new Rect();
  2. getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
  3. int statusBarHeight = rectangle.top ;

④Get the width and height of the View layout area

  1. Rect rect = new Rect();
  2. getWindow().findViewById(Window.ID_ANDROID_CONTENT).getDrawingRect(rect);

2. Android View absolute relative coordinate system

View's static coordinate method

  • getLeft(): Returns the distance from the left side of the View itself to the left side of the parent layout;
  • getTop(): Returns the distance from the top edge of the View itself to the top edge of the parent layout;
  • getRight(): Returns the distance from the right side of the View to the left side of the parent layout;
  • getBottom(): Returns the distance from the bottom edge of the View itself to the top edge of the parent layout;
  • getX(): The return value is getLeft()+getTranslationX(). When setTranslationX() is called, getLeft() remains unchanged, but getX() changes.
  • getY(): The return value is getTop()+getTranslationY(). When setTranslationY() is called, getTop() remains unchanged, but getY() changes.

3. Some methods provided by MotionEvent when a finger touches the screen

  • getX(): The distance between the current touch event and the left side of the current View
  • getY(): The distance between the current touch event and the top edge of the current View
  • getRawX(): The distance between the current touch event and the left side of the entire screen
  • getRawY(): The distance between the current touch event and the top edge of the entire screen

4. View width and height method

  • getWidth() is valid after layout, and the return value is mRight-mLeft;
  • getHeight() is valid after layout, and the return value is mBottom-mTop;
  • getMeasuredWidth() returns the mMeasuredWidth value obtained during the measure process;
  • getMeasuredHeight() returns the mMeasuredHeight value obtained during the measure process;

5. Android View sliding related coordinate system

View sliding method

  • offsetLeftAndRight(int offset)|Move View horizontally. If offset is positive, the x-axis moves in the positive direction. The entire View is moved. getLeft() will change. It is very useful for custom Views.
  • offsetTopAndBottom(int offset)|Move the View vertically. If offset is positive, the y-axis moves in the positive direction. The entire View is moved, and getTop() will change. It is very useful for customizing Views.
  • scrollTo(int x, int y)|Slide the content in the View (not the entire View) to the corresponding position. The reference coordinate origin is the upper left corner of the ParentView. If x and y are positive, move in the opposite direction of the xy axis, and vice versa;
  • scrollBy(int x, int y)|Continue to slide xy based on scrollTo().
  • setScrollX(int value)|Essentially scrollTo(), but only changes the Y-axis sliding.
  • setScrollY(int value)|Essentially scrollTo(), but only changes the X-axis sliding.
  • getScrollX()/getScrollY()|Get the current scroll position offset.

Many people may be confused about the feature that the scrollBy() and scrollTo() parameters of Android View pass positive numbers but move in the negative direction of the coordinate system, or even memorize the conclusion. Here we briefly give the real reason for this feature - source code analysis, as follows:

  1. public void scrollTo( int x, int y) {
  2. if (mScrollX != x || mScrollY != y) {
  3. int oldX = mScrollX;
  4. int oldY = mScrollY;
  5. mScrollX = x;
  6. mScrollY = y;
  7. invalidateParentCaches();
  8. onScrollChanged(mScrollX, mScrollY, oldX, oldY);
  9. if (!awakenScrollBars()) {
  10. postInvalidateOnAnimation();
  11. }
  12. }
  13. }

The method comment of View clearly states that calling it will trigger the onScrollChanged() and invalidated() methods, so let's turn our attention to the draw() process triggered by the invalidated() method. The draw() process will eventually trigger the following invalidate() method, as follows:

  1. public void invalidate( int l, int t, int r, int b) {
  2. final int scrollX = mScrollX;
  3. final int scrollY = mScrollY;
  4. //The real reason why the parameters and coordinates are reversed when using scroller
  5. invalidateInternal(l - scrollX, t - scrollY, r - scrollX, b - scrollY, true , false );
  6. }

The core is here

Special attention to the scrollTo() and scrollBy() methods: If you call the scrollTo() method on a ViewGroup, the content inside the ViewGroup will be scrolled. If you want to scroll a ViewGroup, nest an outer layer for it and scroll the outer layer.

Summarize

  • view gets its own coordinates: getLeft(), getTop(), getRight(), getBottom()
  • View gets its own width and height: getHeight(), getWidth()
  • motionEvent gets coordinates: getX(), getY(), getRawX(), getRawY()
  • Then there is the motionEvent method:
  • getX(): Gets the x-axis coordinate of the click event relative to the left side of the control, that is, the distance from the click event to the left side of the control
  • getY(): Gets the y-axis coordinate of the click event relative to the top edge of the control, that is, the distance between the click event and the top edge of the control
  • getRawX(): Gets the x-axis coordinate of the click event relative to the left side of the entire screen, that is, the distance from the click event to the left side of the entire screen
  • getRawY(): Gets the y-axis coordinate of the click event relative to the top edge of the entire screen, that is, the distance between the click event and the top edge of the entire screen

<<:  Foreign media: If you are still using an older model, it is worth upgrading to iPhone 13

>>:  Automatic login is not available yet! But WeChat has added this useful function

Recommend

Product News | What are the recent important updates of Baidu Promotion?

Commercial products are updated quickly...How can...

Domestic Lexus is included in the plan. FAW Toyota: It will take about ten years

As one of the very few luxury brands that insists...

Year-end review: Major progress in China's space exploration in 2021

Looking back at the vast starry sky in 2021, the ...

Rethinking wearables: The story behind Microsoft's smart wristband out of stock

When the wearable device market represented by sm...

APP user operation: How to prevent users from changing their minds?

Preventing user churn and improving retention and...

How to improve product conversion rate?

When we start promoting a product, we must first ...

3 tips for refined operation strategies!

As the cost of acquiring traffic gradually increa...

Microsoft is in a bad mood: Users complain that Windows 10 is full of troubles

A study by a British consumer protection organiza...

What's going on with the repeated explosions in hit TV series?

Recently popular drama "Beginning" Ther...

[Smart Farmers] How to remove the bitterness of cucumbers? Genomics can help

[Smart Farmers] How to remove the bitterness of c...

Microsoft withdraws first update of Windows 10 Mobile due to numerous bugs

[[159323]] Microsoft is already at a critical jun...

The secret of Haidilao’s marketing success!

Most companies are well aware of the importance o...