Android:如何检查 ScrollView中的View是否可见?

程序中ScrollView试图有一系列View。我想确定这些view哪些是当前可见的,哪些是不可见的。使用下面的这些代码实现不了,我找不出问题所在,请大家帮忙,非常感谢。

 Rect bounds = new Rect(); 
            view.getDrawingRect(bounds); 

            Rect scrollBounds = new Rect(scroll.getScrollX(), scroll.getScrollY(),  
                    scroll.getScrollX() + scroll.getWidth(), scroll.getScrollY() + scroll.getHeight()); 

            if(Rect.intersects(scrollBounds, bounds)) 
            { 
                 //is  visible 
            } 

在你测试的view中使用View#getHitRect代替View#getDrawingRect。 你可以在ScrollView中使用 View#getDrawingRect,而不用去精确的计算。

View#getDrawingRect的代码:

public void getDrawingRect(Rect outRect) { 
        outRect.left = mScrollX; 
        outRect.top = mScrollY; 
        outRect.right = mScrollX + (mRight - mLeft); 
        outRect.bottom = mScrollY + (mBottom - mTop); 
 } 

View#getHitRect的代码:

public void getHitRect(Rect outRect) { 
        outRect.set(mLeft, mTop, mRight, mBottom); 
}