Android - 了解View.getLocalVisibleRect(Rect)

Unk*_*Joe 22 android view

我正在寻找任何理解这种方法的线索.

有中没有信息官方Android的SDK文档了.

它返回什么样的矩形?

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

如果这个视图不可见怎么办?它返回null吗?或者某个带有某种VIEW_INVISIBLE值的矩形?

任何有使用这种方法经验的人都可以帮我吗?

Lea*_*rtS 21

来自getGlobalVisibleRectJavaDoc:

/**
 * 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.
 */
Run Code Online (Sandbox Code Playgroud)

getLocalVisibleRect调用getGlobalVisibleRect,然后按照建议将其设置为local:

r.offset(-offset.x, -offset.y); // make r local`
Run Code Online (Sandbox Code Playgroud)

所以:

  • 它不返回Rectangle,它返回一个布尔值.但它可以设置你传递的矩形的参数,并且必须是android.graphics.Rect矩形;
  • 矩形r将填充局部坐标;
  • 我不确定,但我认为它对于visibile和不可见的视图是相同的,而它应该返回false为视图 visibility="gone"

  • 通常在Android中阅读源代码是尝试弄清楚发生了什么的最好的第一步. (3认同)