Android View's visibility check of getLocalVisibleRect and getGlobalVisibleRect

getGlobalVisibleRect(rect);
uses the upper left corner of the screen as the reference system,
judges that part of the view is on the screen, and returns true (not blocked by the parent View).
Conversely, if it is completely covered by the parent View or is itself invisible, return false.

getLocalVisibleRect(rect);
When the View is visible, the upper left corner of its own View is used as the reference system, and the origin of the coordinate system is the coordinate origin of the View itself.
When the View is invisible, the upper left corner of the parent control is used as the reference system, and the origin of the coordinate system is the coordinate origin of the parent control of the View.

For a better understanding, the demo is as follows: (The resolution of the test machine is 720*1440, and the orange-red view is 400*400px. The layout is a RelativeLayout nested with this TextView)

The screen contains a blue status bar! ! The height is 55px, so the calculation result should take this issue into account

When Textview sets marginLeft="100px", marginTop="100px":

When Textview sets marginLeft="-100px", marginTop="-100px":

When Textview sets marginRight="-100px", marginBottom="-100px":

The following understanding is mainly for the case where getLocalVisibleRect(rect) and View are not visible:

When Textview sets marginTop="-500px", TextView is outside the screen at this time:

       

At this time, getLocalVisibleRect(rect), the origin of the coordinate system is the coordinate origin of its parent control, which is the outer nested RelativeLayout, the width and height of RelativeLayout are match_parent, so its value is the same as getGlobalVisibleRect(rect) .


When Textview sets layout_marginBottom="-500px", layout_marginRight="-500px", both at the bottom of the parent layout, the TextView is outside the screen at this time:

The height should consider the bottom virtual navigation bar! ! The height is 55px, so the calculation result should take this issue into account

      

At this time, getLocalVisibleRect(rect), the origin of the coordinate system is the coordinate origin of its parent control, which is the outer nested RelativeLayout, the width and height of RelativeLayout are match_parent, so its value is the same as getGlobalVisibleRect(rect) .

The width and height of TextView are 400px, layout_marginBottom="-500px", layout_marginRight="-500px",
so the distance between TextView and the right side of the screen is 100px, and the distance from the bottom of the screen is
100px. Considering that the height of the lower virtual navigation bar is 55px, so TextView The actual distance from the bottom of the screen is (100px-55px)=45px

Left: 720+100 =820px
Above: 1440+45=1485px
Right: 720+100+400 =1220px
Below: 1440+45+400 =1885px

Judging whether View1 is in a View2 visible area
When using getLocalVisibleRect(rect), when View is not in the visible area:
at the top, Rect.top <0
is at the bottom, Rect.bottom>View2.getHeight
so
fun isCover( activity: Activity, view1: View, view2: View): Boolean {         val rect = Rect()         view1.getLocalVisibleRect(rect)         return !(rect.top<0||rect.bottom>view2.height) }



Determine the percentage of View's visible area (called when view1 starts to be visible, otherwise it will always be 100%)
fun getVisibilePercent(activity: Activity, view1: View): Int {         val rect = Rect()         view1.getLocalVisibleRect(rect )         Log.e("Test", "rect.height = "+rect.height().toString() + " view1.height = "+view1.height)        val a = rect.height() * 100 / view1. height         Log.e("Test", a.toString())         return a }






...... 

Determine whether the View appears on the screen
fun isCoverScreen_LocalVisibleRect(view1: View): Boolean {         val screenWidth = CFUtil.getScreenWidth()//Get the screen width         val screenHeight = CFUtil.getScreenHeight()//Get the screen height         val rect = Rect()         view1.getLocalVisibleRect(rect)         return !(rect.top<0||rect.bottom>screenHeight) }





Summarize

getGlobalVisibleRect(rect) is simply the mapping of the target view to the parent view,
and then calculates from the upper left corner of the screen, and saves it in the rect. Note that it is the parent view, not the screen, because the demo's parent view (RelativeLayout) and screen width and height it's the same.

getLocalVisibleRect(rect) As long as the upper left corner of the View is on the screen, the coordinates of the upper left corner of its LocalVisibleRect must be (0,0).
If the lower right corner of the View is on the screen, the coordinates of the lower right corner of its LocalVisibleRect must be (view .getWidth(), view.getHeight()).
If it is not on the screen, its Rect value is the same as getGlobalVisibleRect(rect).

Guess you like

Origin blog.csdn.net/NewActivity/article/details/127498502