1

我试图指出某个视图(比如视图 A)是否 100% 可见,这意味着,如果它是 100x100 视图,我想确保显示所有 10000 像素。

样本:

--------------
|            |
|   ___      |
|   |A|      |
|   ---      |
|            |
--------------

应该返回100 % 而:

--------------
|            |
|   _________|_________
|   |A       |         |
|   ---------|---------
|            |
--------------

应该返回大约 50%。

我试过测量 globalVisibleRect、localVisibleRect、hitTest、drawableRect、focusableRect,无论视图是否完全可见,它们都是一样的。

有任何想法吗?

4

2 回答 2

1

如果你得到视图的坐标,你可以很容易地计算出交叉点的面积:

intersectionArea = max(0, max(AX2, BX2) - min(AX1, BX1)) * max(0, max(AY2, BY2) - min(AY1, BY1))

由此,您可以计算联合使用的面积:

unionArea = AreaA + AreaB - intersectionArea

然后你可以确定这个区域的比例

intersectionArea / unionArea

于 2015-11-23T15:40:41.773 回答
0

https://math.stackexchange.com/questions/99565/simplest-way-to-calculate-the-intersect-area-of-two-rectangles

正确答案是这个---->>>

x_overlap = Math.max(0, Math.min(rect1.right, rect2.right) - Math.max(rect1.left, rect2.left)); y_overlap = Math.max(0, Math.min(rect1.bottom, rect2.bottom) - Math.max(rect1.top, rect2.top)); 重叠区域 = x_overlap * y_overlap;

于 2020-07-05T12:59:44.793 回答