Android View.getLocationOnScreen无法正常工作

Android View.getLocationOnScreen无法正常工作,android,view,coordinate-systems,coordinate-transformation,Android,View,Coordinate Systems,Coordinate Transformation,我希望将视图的边界投影/转换为屏幕坐标。局部边界为0,01280628,我的视图上方的操作栏为108像素高 问题是view.getLocationOnScreen将第一个点转换为0x108,这是正确的。但它也将所有其他角点(1280x0、1280x628、0x628)再次转换为0x180。知道为什么吗 这是我的密码 public static void getLocationOnScreen(View view, final Rect src, Rect dst) { if (view

我希望将视图的边界投影/转换为屏幕坐标。局部边界为0,01280628,我的视图上方的操作栏为108像素高

问题是view.getLocationOnScreen将第一个点转换为0x108,这是正确的。但它也将所有其他角点(1280x0、1280x628、0x628)再次转换为0x180。知道为什么吗

这是我的密码

public static void getLocationOnScreen(View view, final Rect src, Rect dst) {
    if (view == null || src == null)
        return;

    if (dst == null)
        dst = new Rect();

    Point[] corners = RectTools.getCornerPoints(src);
    int[] location = new int[2];

    for (int i = 0; i < corners.length; i++) {
        Point current = corners[i];
        location[0] = current.x;
        location[1] = current.y;

        view.getLocationOnScreen(location);

        corners[i] = new Point(location[0], location[1]);
    }

    createRectangle(dst, corners);
}
publicstaticvoid getLocationOnScreen(视图、最终Rect src、Rect dst){
如果(视图==null | | src==null)
返回;
如果(dst==null)
dst=新的Rect();
Point[]corners=RectTools.getCornerPoints(src);
int[]位置=新int[2];
对于(int i=0;i
好的,我只是理解错了函数-可能是因为我来自Microsoft.NET C#world,在那里你可以轻松地将坐标从控件转换到屏幕,反之亦然。无论如何,view.getLocationOnScreen只是返回视图的左上角。所以你必须用它作为偏移量

public static void getLocationOnScreen(View view, final Rect src, Rect dst) {
    if (view == null || src == null)
        return;

    if (dst == null)
        dst = new Rect();

    int[] location = new int[2];
    view.getLocationOnScreen(location);
    int offsetX = location[0];
    int offsetY = location[1];

    dst.set(src.left + offsetX, src.top + offsetY, src.right + offsetX,
            src.bottom + offsetY);
}