The official documentation

If some regions of the view are not clipped by the parent view, then returns these regions through r according to the global (root) coordinates.

Offset r.offset(-globaloffset.x, -Globaloffset.y) if returning from the current coordinates through r.

Return false if the view is completely clipped by the parent view or panned out

When a View is partially or fully displayed:

The region shown at getGlobalVisibleRect is represented by Rect at the origin of the coordinates in the upper-left corner of the screen

The region shown at getLocalVisibleRect is represented by Rect at its upper-left corner as the origin of the coordinates

There is an offset between the upper left corner of the screen and the upper left corner of the View. In partial or complete view cases, the Rect returned by getLocalVisibleRect is equal to the offset of left, top, right, and bottom of the Rect returned by getGlobalVisibleRect.

The top left corner of the screen is (0,0), and the top left corner of view itself is at (500,800) on the screen

GlobalOffset. X = 500; GlobalOffset. Y = 500.

When the Rect returned by getGlobalVisibleRect is (500,800,550,900)

And getLocalVisibleRect return Rect for Global left, right-500top, bottom-800, (0,0,50,100)

Two methods with getLocationInWindow InSurface/OnScreen (for positioning in the target container: int []). In the view. The best getViewTreeObserver (.) addOnGlobalLayoutListener view is put after the completion of use.

application

Both methods have Boolean returns that determine whether the view is displayed or not.

The view covered by the same level of view is judged to be displayed by the two methods, and the view covered by the same level only counts as repeated drawing. The area beyond the parent view is clipped by the parent view. Or the child view moves out of the parent view.

In real development, the view will not only be displayed in the case of its display area. It also gets its returned Rect without showing it. For example, the horizontal TAB automatically moves beyond the screen.

View can be completely invisible, partially or completely visible in the case of a single horizontal or vertical swipe:

  • The recTS returned by getLocalVisibleRect and getGlobalVisibleRect are the same when the View is not displayed or completely removed, i.e. completely invisible.

    • Left, top, right, bottom The two items that change with the direction of movement change at the top left corner of the screen as the origin of the coordinates;

    • The other two terms, which do not change, take the upper left corner of view itself as the relative value of the coordinate origin and remain unchanged.

      For example, when moving horizontally, the values of left and right will change with reference to the left origin, and the values of top and bottom will take 0 and height unchanged with reference to themselves

  • The Rect returned when the View is partially or fully visible is:

    • getGlobalVisibleRectDisplay areaIn order toTop left corner of the screenIs the Rect representation of the origin of the coordinates (remember that the two terms in Rect that do not change with view are also replaced by the absolute coordinates in the upper left corner of the screen)
    • getLocalVisibleRectDisplay areaIn order toTop left corner of itselfIs the Rect representation of the coordinates origin
  • The Rect returned by getLocalVisibleRect changes in particular when the View changes between not showing at all and partial or full display, such as when the coordinates are about to start entering the screen from the positive direction or when the View is about to move off the screen completely.

    • Before View enters the screen: getLocalVisibleRect returns the two Rect items that change with the direction of movement, with the top left corner of the screen as the origin of the coordinates. Left =ScreenWidth, right=ScreenWidth+ViewWidth(top=ScreenHeight, bottom=ScreenHeight+ view)

    • When View enters the screen: getLocalVisibleRect returns the two terms of Rect that change with the direction of movement and change with its upper left corner as the origin of the coordinates. Left =0, right=0, top=0, bottom=0

      As soon as the view enters the screen, the two terms of the returned Rect, which change with the direction, change dramatically. Width changed from screenWidth to 0

    • Before View is completely removed from the screen: getLocalVisibleRect uses its upper-left corner as the origin, irrelevant to the origin of the screen, so it doesn’t matter if its upper-left corner is already outside the screen. Left = viewWidth and right= viewWidth

    • When View is completely removed from the screen: getLocalVisibleRect returns two items in Rect that change with the direction of movement, with the top left corner of the screen as the origin of the coordinates. Left = – viewwidth, right = 0

      The moment the view is completely removed from the screen, the left in the returned Rect changes from viewWidth to -viewWidth

For example, the change in the left of the Rect returned by getLocalVisibleRect when the view enters the screen horizontally from outside the screen and is completely removed from the screen

It’s way off the screen Before I go to the screen When I get on the screen Before it’s completely removed When it’s completely removed Continue to move
up screenWidth 0 width -width – up

Conclusion:

When the View is not displayed at all, the two items return the same Rect, and the two items that change with the direction of movement change with reference to the upper left corner of the screen; The two terms that don’t change stay the same with reference to their top left corner.

When View is partially or completely displayed, the region displayed by getGlobalVisibleRect is represented by Rect at the origin of coordinates in the upper left corner of the screen; The region shown at getLocalVisibleRect is represented by Rect at its upper-left corner as the origin of the coordinates. The two are interchangeable by adding or subtracting the offset between the top-left corner of the screen and the top-left corner of the view from the returned Rect.

The Rect returned by getLocalVisibleRect switches between the top left and the top left of the screen in different reference frames, resulting in a special adjacency value change when the View changes between not showing at all and partially or completely.