Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the android equivalent of UIView's convertRect / convertPoint functions?

UIView has the following:

- convertPoint:toView:
- convertPoint:fromView:
- convertRect:toView:
- convertRect:fromView:

What is the Android equivalent? More generally, given two Views, how do I get the second View's rect in the coordinate system of the first?

like image 832
i_am_jorf Avatar asked Jul 23 '15 04:07

i_am_jorf


1 Answers

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;
}
like image 98
tachyonflux Avatar answered Nov 08 '22 23:11

tachyonflux