UIView具有以下内容:
- convertPoint:toView:
- convertPoint:fromView:
- convertRect:toView:
- convertRect:fromView:
有什么Android的等效方法吗?更普遍地说,给定两个View
,如何在第一个View
的坐标系中获取第二个View
的矩形?
UIView具有以下内容:
- convertPoint:toView:
- convertPoint:fromView:
- convertRect:toView:
- convertRect:fromView:
有什么Android的等效方法吗?更普遍地说,给定两个View
,如何在第一个View
的坐标系中获取第二个View
的矩形?
我认为sdk中没有类似的功能,但是看起来你可以使用getLocationOnScreen
自己很容易地编写实现:
我不认为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 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;
}
实现这个的方法是使用getGlobalVisibleRect(Rect r, Point globalOffset)。
/**
* If some part of this view is not clipped by any of its parents, then
* return that area in r in global (root) coordinates. To convert r to local
* coordinates (without taking possible View rotations into account), offset
* it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
* If the view is completely clipped or translated out, return false.
*
* @param r If true is returned, r holds the global coordinates of the
* visible portion of this view.
* @param globalOffset If true is returned, globalOffset holds the dx,dy
* between this view and its root. globalOffet may be null.
* @return true if r is non-empty (i.e. part of the view is visible at the
* root level.
*/
你可以传递一个Point
对象,来获取相对于根View
的偏移量。关键在于各个视图都会相对于一个共同的项目获得位置。然后你可以使用这个偏移量来计算相对位置。文档指出,要想获取本地坐标,需要用-globalOffset去偏移Rect
。如果你想要获取第二个视图在第一个视图中的Rect
坐标,则需要用第二个视图的-globalOffset去偏移第一个视图的Rect
。
类似地,如果你知道点在根视图中的坐标,也可以使用globalOffset
将其转换为相对坐标。这应该是一个减法运算:anyPoint - globalOffset
。
记得在进行计算之前,检查getGlobalVisibleRect()
是否返回true以及globalOffset
是否为null。
在Android中可能没有完全相同的方法
但是,您可以通过使用
View.getLocationOnScreen(int[] location)
和View.getLocationInWindowint[] location()
来实现
getLocationOnScreen(int[] location)
计算此视图在其窗口中的坐标。
getLocationInWindow(int[] location)
计算此视图在屏幕上的坐标。
或者 View.getLeft() 和 View.getTop()
它们是:
getLeft()
该视图相对于其父视图的左侧位置。
getTop()
该视图相对于其父视图的顶部位置。
private int getRelativeLeft(View myView) {
if (myView.getParent() == myView.getRootView())
return myView.getLeft();
else
return myView.getLeft() + getRelativeLeft((View) myView.getParent());
}
private int getRelativeTop(View myView) {
if (myView.getParent() == myView.getRootView())
return myView.getTop();
else
return myView.getTop() + getRelativeTop((View) myView.getParent());
}