gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:24:08 26 4
gpt4 key购买 nike

UIView 具有以下内容:

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

Android 的等价物是什么?更一般地说,给定两个 View,如何在第一个 View 的坐标系中获取第二个 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 - UIView 的 convertRect/convertPoint 函数的 android 等价物是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31578204/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com