gpt4 book ai didi

android - 检测 View 是否重叠

转载 作者:IT老高 更新时间:2023-10-28 23:38:02 26 4
gpt4 key购买 nike

我在其他尺寸的屏幕上绘制 View 时遇到问题!我需要具有两个 View 类型参数的方法。如果第一个 View 与第二个 View 重叠,则返回 true,在另一种情况下返回 false!

enter image description here

enter image description here

最佳答案

您还可以使用 Rect.intersect() 来查找重叠 View 。

    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);

关于android - 检测 View 是否重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26252710/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com