/**
* 可见区域小于面积的50%,为覆盖
* @param view
* @return
* 参考https://blog.csdn.net/xuguobiao/article/details/50911986
*/
public static boolean isCover(View view){
View currentView = view;
Rect currentViewRect = new Rect();
boolean partVisible = currentView.getGlobalVisibleRect(currentViewRect);
boolean halfPercentVisible = (currentViewRect.bottom - currentViewRect.top) * (currentViewRect.right - currentViewRect.left) >= (view.getMeasuredHeight() * view.getMeasuredWidth() / 2);
boolean totalViewVisible = partVisible && halfPercentVisible;
//被其父布局遮挡超过一半
if (!totalViewVisible)
return true;
while (currentView.getParent() instanceof ViewGroup) {
ViewGroup currentParent = (ViewGroup) currentView.getParent();
if (currentParent.getVisibility() != View.VISIBLE) {
return true;
}
int start = indexOfViewInParent(currentView, currentParent);
for (int i = start + 1; i < currentParent.getChildCount(); i++) {
Rect viewRect = new Rect();
view.getGlobalVisibleRect(viewRect);
View otherView = currentParent.getChildAt(i);
if (otherView.getVisibility() != View.VISIBLE)
continue;
Rect otherViewRect = new Rect();
otherView.getGlobalVisibleRect(otherViewRect);
//如果相交
if (Rect.intersects(viewRect, otherViewRect)){
int width=Math.min(viewRect.right,otherViewRect.right)-Math.max(viewRect.left, otherViewRect.left);
int height=Math.min(viewRect.bottom, otherViewRect.bottom)-Math.max(viewRect.top, otherViewRect.top);
//相交部分的2倍大于view本身大小
//参考//https://blog.csdn.net/u012476249/article/details/53318891
if(width *height*2 >= view.getMeasuredHeight() * view.getMeasuredWidth()){
return true;
}
}
}
currentView = currentParent;
}
return false;
}