判断视图是否完整可见

知识点:

From the 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 calls getGlobalVisibleRect and then makes it local as suggested:

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

So:

  • It doesn't return a Rectangle, it returns a boolean. But it can set the parameters of a rectangle you pass, and that must be an android.graphics.Rect rectangle;
  • The rectangle r will be filled with local coordinates;
  • I'm not sure but I think it's the same for visibile and invisible views, while it should return false for views with visibility="gone"

详见http://www.myexception.cn/android/1884660.html


private boolean isViewCompletelyVisible(View view) {
    Rect scrollBounds = new Rect();
    mRecyclerView.getHitRect(scrollBounds);
    if (view.getLocalVisibleRect(scrollBounds)) {
        //此控件至少有一个像素在可视范围内
        Rect rect = new Rect();
        view.getGlobalVisibleRect(rect);
        if (YoukuUtil.isPad()) {
            if (view.getHeight() <= (rect.bottom - rect.top) &&
                    (rect.top >= 0 && rect.top < YoukuUtil.getScreenRealWidth(getActivity().getApplicationContext())) &&
                    (rect.bottom > 0 && rect.bottom <= YoukuUtil.getScreenRealWidth(getActivity().getApplicationContext()))) {
                return true;
            }
        } else {
            if (view.getHeight() <= (rect.bottom - rect.top) &&
                    (rect.top >= 0 && rect.top < YoukuUtil.getScreenRealHeight(getActivity().getApplicationContext())) &&
                    (rect.bottom > 0 && rect.bottom <= YoukuUtil.getScreenRealHeight(getActivity().getApplicationContext()))) {
                return true;
            }
        }
    }
    return false;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值