【问题标题】:What is the android equivalent of UIView's convertRect / convertPoint functions?UIView 的 convertRect / convertPoint 函数的 android 等价物是什么?
【发布时间】:2015-10-13 05:14:08
【问题描述】:
UIView 有以下内容:
- convertPoint:toView:
- convertPoint:fromView:
- convertRect:toView:
- convertRect:fromView:
什么是 Android 等价物?更一般地,给定两个Views,我如何在第一个坐标系中获得第二个View 的矩形?
【问题讨论】:
标签:
android
coordinate-transformation
【解决方案1】:
我认为 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;
}
【解决方案2】:
在 Android 中它可能没有完全一样的方法
但是,你可以这样做
View.getLocationOnScreen(int[] location) 和 View.getLocationInWindowint[] location()
哪个
getLocationOnScreen(int[] location)
计算此视图的坐标
它的窗口。
getLocationInWindow(int[] location)
计算此视图的坐标
屏幕。
或 View.getLeft() 和 View.getTop()
哪个
getLeft()
此视图相对于其父视图的左侧位置。
getTop()
此视图相对于其父视图的顶部位置。
对于你的情况,我会使用 getLeft() 和 getTop() 来查找它在 2 View 之间的空间并获取它的范围
link 有一个示例,说明如何使用 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());
}
【解决方案3】:
方法是使用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,则使用 second 视图的 -globalOffset 偏移 first 视图的 Rect。
同样,如果您知道它在根视图中的坐标,您可以使用globalOffset 将一个点转换为相对坐标。这应该是一个减法:anyPoint - globalOffset。
请记住在进行计算之前检查 getGlobalVisibleRect() 返回 true 并且 globalOffset 不为空。