什么是UIView的convertRect/convertPoint函数的android等价物?

i_a*_*orf 13 android coordinate-transformation

UIView有以下内容:

- convertPoint:toView:
- convertPoint:fromView:
- convertRect:toView:
- convertRect:fromView:
Run Code Online (Sandbox Code Playgroud)

什么是Android等价物?更一般地说,给定两个Views,如何View在第一个坐标系中获得第二个矩形?

tac*_*lux 14

我不认为有一个等价的sdk,但似乎你可以很容易地使用编写自己的实现 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;
}
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)