UIView具有以下内容:
- convertPoint:toView: - convertPoint:fromView: - convertRect:toView: - convertRect:fromView:
什么是Android相当的?更一般地说,给定两个视图,我如何得到第二个View的rect在第一个坐标系?
解决方法
我不认为这是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; }
public static Rect convertRect(Rect fromrect,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; }