UIView has the following:
- convertPoint:toView:
- convertPoint:fromView:
- convertRect:toView:
- convertRect:fromView:
What is the Android equivalent? More generally, given two View
s, how do I get the second View
's rect in the coordinate system of the first?
I don't think there is an equivalent as part of the sdk, but it seems like you could write your own implementation very easily using getLocationOnScreen
:
public static Point convertPoint(Point fromPoint, View fromView, View toView){
int[] fromCoord = new int[2];
int[] toCoord = new int[2];
fromView.getLocationOnScreen(fromCoord);
toView.getLocationOnScreen(toCoord);
Point toPoint = new Point(fromCoord[0] - toCoord[0] + fromPoint.x,
fromCoord[1] - toCoord[1] + fromPoint.y);
return toPoint;
}
public static Rect convertRect(Rect fromRect, View fromView, View toView){
int[] fromCoord = new int[2];
int[] toCoord = new int[2];
fromView.getLocationOnScreen(fromCoord);
toView.getLocationOnScreen(toCoord);
int xShift = fromCoord[0] - toCoord[0];
int yShift = fromCoord[1] - toCoord[1];
Rect toRect = new Rect(fromRect.left + xShift, fromRect.top + yShift,
fromRect.right + xShift, fromRect.bottom + yShift);
return toRect;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With