android 多层级检测view被遮挡是否超过一半

 /**
     * 可见区域小于面积的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;
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Java中解决元素层级遮挡的方法有多种。以下是一些常见的方法: 1. 使用布局管理器(Layout Manager):Java提供了多个布局管理器,可以根据需要选择适合的布局来处理元素的层级关系。布局管理器可以自动处理元素的位置和大小,确保元素不会互相遮挡。 2. 调整组件的位置和大小:可以通过修改组件的坐标和尺寸来调整元素的层级关系。可以使用setBounds()方法或者其它相关的方法来实现。 3. 使用透明度(Opacity):通过设置组件的透明度,可以实现元素之间的覆盖和透明效果。可以使用setOpaque()方法将组件设置为透明,然后使用setAlpha()方法设置透明度的值。 4. 使用图层(LayeredPane):使用JLayeredPane可以创建多个图层,每个图层上可以放置一个或多个组件。通过调整图层的顺序,可以改变元素的层级关系。可以使用add()和setLayer()方法来添加和移动组件到不同的图层上。 5. 使用重叠窗体(Dialog):可以使用Java的对话框(Dialog)功能来实现元素之间的层叠效果。对话框可以有不同的层级,可以通过修改对话框的模态性和焦点来控制元素的层级关系。 6. 使用JavaFX:如果使用JavaFX来开发界面,可以使用StackPane来处理元素的层级关系。StackPane可以通过设置每个子组件的对齐方式和层级来控制元素的显示和遮挡关系。 总之,Java中有多种方法可以解决元素层级遮挡的问题,具体方法的选择取决于具体的需求和实现方式。以上提到的方法仅供参考,开发者可以根据实际情况选择合适的方法来解决问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值