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

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

UIView 具有以下内容:

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

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

我认为 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;
}

在Android中可能没有完全一样的方法

但是,您可以通过

View.getLocationOnScreen(int[] location)View.getLocationInWindowint[] location()

哪个

getLocationOnScreen(int[] location)

Computes the coordinates of this view in its window.

getLocationInWindow(int[] location)

Computes the coordinates of this view on the screen.


或View.getLeft() 和View.getTop()

哪个

getLeft()

Left position of this view relative to its parent.

getTop()

Top position of this view relative to its parent.


对于你的情况,我会使用 getLeft() 和 getTop() 来找到它 space 在 2 个视图之间并获取它的范围

这个 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());
}

方法是 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,则使用 [=29= 的 -globalOffset 偏移 第一个 视图的 Rect ]第二个视图。

同样,如果您知道根视图中的坐标,则可以使用 globalOffset 将点转换为相对坐标。这应该是一个减法:anyPoint - globalOffset.

记得在计算之前检查 getGlobalVisibleRect() 是否返回 true 并且 globalOffset 不为空。