什么是android相当于UIView的convertRect / convertPoint函数?

前端之家收集整理的这篇文章主要介绍了什么是android相当于UIView的convertRect / convertPoint函数?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
UIView具有以下内容
- convertPoint:toView:
- convertPoint:fromView:
- convertRect:toView:
- convertRect:fromView:

什么是Android相当的?更一般地说,给定两个视图,我如何得到第二个View的rect在第一个坐标系?

解决方法

我不认为这是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 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相关文章