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 - DisplayMetrics metrics = new DisplayMetrics();
- getWindowManager().getDefaultDisplay().getMetrics(metrics);
- int widthPixels = metrics.widthPixels;
- int heightPixels = metrics.heightPixels;
②Get the width and height of the application area - Rect rect = new Rect();
- getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
③Get the status bar height - Rect rect = new Rect();
- getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
- int statusBarHeight = rectangle.top ;
④Get the width and height of the View layout area - Rect rect = new Rect();
- 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: - public void scrollTo( int x, int y) {
- if (mScrollX != x || mScrollY != y) {
- int oldX = mScrollX;
- int oldY = mScrollY;
- mScrollX = x;
- mScrollY = y;
- invalidateParentCaches();
- onScrollChanged(mScrollX, mScrollY, oldX, oldY);
- if (!awakenScrollBars()) {
- postInvalidateOnAnimation();
- }
- }
- }
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: - public void invalidate( int l, int t, int r, int b) {
- final int scrollX = mScrollX;
- final int scrollY = mScrollY;
- //The real reason why the parameters and coordinates are reversed when using scroller
- invalidateInternal(l - scrollX, t - scrollY, r - scrollX, b - scrollY, true , false );
- }
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
|