//view被遮住
private fun isCover(): Boolean {
val rect: Rect = Rect()
val cover = getGlobalVisibleRect(rect)
if (cover) {
if (rect.width() >= measuredWidth && rect.height() >= measuredHeight) {
return !cover
}
}
return true
}
//当 View 有一点点不可见时立即返回false
private fun isVisibleLocal(): Boolean {
val rect = Rect()
getLocalVisibleRect(Rect())
return rect.top == 0
}
//view显示在屏幕大于一半返回true
fun checkIsMajorityVisible(view: View?): Boolean {
if (view == null) return false
val rect = Rect()
val location = IntArray(2)
view.getLocationInWindow(location)
view.getLocalVisibleRect(rect)
val showHeight = rect.bottom - rect.top
val height = view.height.orZero()
return showHeight.toFloat() > height.toFloat() / 2
}
其中在view往上滑动时,头部慢慢被遮住了,
此时rect.bottom = height, 而rect.top由0开始变大
当view处于屏幕中,完全可见时,
rect.bottom = height , rect.top = 0
当view向下滑动时,底部慢慢被遮住了,
此时rect.bottom由height慢慢变小, 而rect.top一直为0
当view有一点可见rect.top总是为0的
//在view内部检测是否屏幕可见,不太可靠
override fun onWindowVisibilityChanged(visibility: Int) {
super.onWindowVisibilityChanged(visibility)
if (visibility == View.VISIBLE) {
if (isVisibleLocal()) {
//todo
}
}
}
网友评论