1

I'm trying to indicate whether a certain view (say - view A) is 100% visible, meaning, if it's a 100x100 view, I want to make sure all 10000 pixels are shown.

Sample:

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

Should return 100% whereas:

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

Should return roughly 50%.

I've tried measuring globalVisibleRect, localVisibleRect, hitTest, drawableRect, focusableRect, and they are all the same, no matter if the view is totally visible or not.

Any ideas?

Daniel
  • 935
  • 2
  • 6
  • 11

2 Answers2

1

If you get the co-ordinates of the views, you can easily compute the area of the intersection:

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

From this, you can compute the area used by the union:

unionArea = AreaA + AreaB - intersectionArea

And you can then determine the ratio of this area

intersectionArea / unionArea

Ed Holloway-George
  • 5,092
  • 2
  • 37
  • 66
  • Thanks, Ed. I thought of a more generic way, without having to iterate throughout all the views I have in my Activity. – Daniel Nov 24 '15 at 07:14
0

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

The right answer is this ---->>>

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)); overlapArea = x_overlap * y_overlap;

Rob
  • 886
  • 9
  • 16