2012-05-04 29 views
22

我正在寻找任何线索来理解这种方法。Android - 理解View.getLocalVisibleRect(Rect)

official Android's SDK documentation没有关于它的信息。

它返回什么样的矩形?

它是否充满了像MotionEvent这样的Raw coorinates?

如果此视图不可见,该怎么办?它是否返回null?或者是一些带有某种VIEW_INVISIBLE值的矩形?

有没有使用这种方法的经验的人能帮我一把吗?

+0

我创建了一个问题的文件。您可以在这里跟踪https://issuetracker.google.com/issues/73832083 顺便说一句,如果您发现缺少或不适当的东西,您也可以为文档创建问题。在页面的最底部有'support'部分。在那里你可以选择“报告文档错误”。 –

回答

20

JavaDoc of getGlobalVisibleRect

/** 
* If some part of this view is not clipped by any of its parents, then 
* return that area in r in global (root) coordinates. To convert r to local 
* coordinates (without taking possible View rotations into account), offset 
* it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)). 
* If the view is completely clipped or translated out, return false. 
* 
* @param r If true is returned, r holds the global coordinates of the 
* visible portion of this view. 
* @param globalOffset If true is returned, globalOffset holds the dx,dy 
* between this view and its root. globalOffet may be null. 
* @return true if r is non-empty (i.e. part of the view is visible at the 
* root level. 
*/ 

getLocalVisibleRect调用getGlobalVisibleRect然后使得当地的建议:

r.offset(-offset.x, -offset.y); // make r local` 

所以:

  • 它不返回一个矩形,它返回一个布尔值。但它可以设置您传递的矩形的参数,并且必须是android.graphics.Rect矩形;
  • 矩形r将填充本地坐标;
  • 我不知道,但我认为这是分享范围和无形的观点相同的,而应该对视图返回false与visibility="gone"
+0

非常感谢您的回答。也许,Android源代码中有更多有关此方法的答案。 – UnknownJoe

+3

通常在Android中阅读源代码是尝试弄清楚发生了什么的最好的第一步。 –