Mat*_*ias 2 android view coordinate-systems coordinate-transformation
我想将视图的边界投影/转换为屏幕坐标。局部范围是0,0,1280,628,正好在我的视图上方的ActionBar高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);
}
Run Code Online (Sandbox Code Playgroud)
好的,我只是错误地理解了函数-也许是因为我来自Microsoft .NET C#世界,在这里您可以轻松地将坐标从控件转换为屏幕,反之亦然。无论如何,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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4757 次 |
| 最近记录: |