detect if views are overlapping

前端 未结 6 1397
名媛妹妹
名媛妹妹 2020-12-13 14:59

I have problem with drawing views on another size screens! I need method which has two parameters of View type. And return true if first view overlapping on second view, an

6条回答
  •  天涯浪人
    2020-12-13 15:29

    You can also use Rect.intersect() to find overlapping views.

        int[] firstPosition = new int[2];
        int[] secondPosition = new int[2];
    
        firstView.getLocationOnScreen(firstPosition);
        secondView.getLocationOnScreen(secondPosition);
    
        // Rect constructor parameters: left, top, right, bottom
        Rect rectFirstView = new Rect(firstPosition[0], firstPosition[1],
                firstPosition[0] + firstView.getMeasuredWidth(), firstPosition[1] + firstView.getMeasuredHeight());
        Rect rectSecondView = new Rect(secondPosition[0], secondPosition[1],
                secondPosition[0] + secondView.getMeasuredWidth(), secondPosition[1] + secondView.getMeasuredHeight());
        return rectFirstView.intersect(rectSecondView);
    

提交回复
热议问题