I'm trying to calculate the rectangle which wraps a group of elements. For example I have a couple of elements with different widths and I have to calculate the rectangle which is wrapping them. Could you help me with this calculation ?
Asked
Active
Viewed 53 times
1 Answers
2
You can calculate it:
Take the minimum of x and y coordinates of every element. That will be the top left corner of the Rectangle.
Then take the maximum of x+width, and y+height of every element. That will be the bottom right corner of the Rectangle.
Or you can let Android calculate it:
Wrap those elements with a FrameLayout. Set both dimensions of the frame to wrap_content. This way the frame will be the rectangle that you are looking for. (This solution assumes that there are no margin on the wrapped items.)

kupsef
- 3,357
- 1
- 21
- 31
-
Hi kupsef, thanks for you answer. The solution with wrapping all elements in a layout is not usable in my case - this would be the easiest solutions, just to wrap them :) I should take the maximum x and width of every element, right ? In fact now I'm facing some problems with calculation of the X & Y and width and height of a TextView. If element is TextView - Y always in 0 no matter where is it position. And because I want to check if I clicked on this rectangle, the second click the width of the TextView is different. I think that is because the width/height is "wrap_content" – StoyanM May 07 '14 at 07:14
-
You should take the maximum of (x+width) and (y+height) to determine the bottom right corner. If you want to measure your views, you have to let android measue it first. See this post: [Link](http://stackoverflow.com/questions/3602026/linearlayout-height-in-oncreate-is-0) – kupsef May 07 '14 at 08:06
-
Thanks, kupsef. The problem with the TextView was because I was using an empty TextView in the layout without to notice. – StoyanM May 07 '14 at 08:17