android view相对于根布局的坐标获取
来源:互联网 发布:vr拼接软件 编辑:程序博客网 时间:2024/06/07 15:17
android之View坐标系看下图就明白了:
引自官方文档:
Position
The geometry of a view is that of a rectangle. A view has a location, expressed as a pair of left and top coordinates, and two dimensions, expressed as a width and a height. The unit for location and dimensions is the pixel.It is possible to retrieve the location of a view by invoking the methods getLeft() and getTop(). The former returns the left, or X, coordinate of the rectangle representing the view. The latter returns the top, or Y, coordinate of the rectangle representing the view. These methods both return the location of the view relative to its parent. For instance, when getLeft() returns 20, that means the view is located 20 pixels to the right of the left edge of its direct parent.
In addition, several convenience methods are offered to avoid unnecessary computations, namely getRight() and getBottom(). These methods return the coordinates of the right and bottom edges of the rectangle representing the view. For instance, calling getRight() is similar to the following computation: getLeft() + getWidth() (see Size for more information about the width.)
放送一段代码,在复杂的UI嵌套中,想要获得当前View相对于activity根布局的x,y坐标的代码段:
private float getX(View v) { if (v != null) { return v.getLeft() + getParentX(v.getParent()); } return 0; } private float getParentX(ViewParent parent) { if (parent != null && parent instanceof ViewGroup) { return ((ViewGroup) parent).getLeft() + getParentX(parent.getParent()); } return 0; } private float getY(View v) { if (v != null) { return v.getTop() + getParentY(v.getParent()) - getSystemBarHeight(); } return 0; } private float getParentY(ViewParent parent) { if (parent != null && parent instanceof ViewGroup) { return ((ViewGroup) parent).getTop() + getParentY(parent.getParent()); } return 0; } private float getSystemBarHeight() { Rect rectangle = new Rect(); mActivity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rectangle); return rectangle.top; }
- android view相对于根布局的坐标获取
- android 获取View的坐标
- Android 获取View的坐标
- 获取相对于窗口(window)的坐标
- 获取cell/section相对于tableView 的坐标
- Android中View的坐标矩形获取
- android应用程序如何获取View的坐标
- android 获取view的位置坐标
- Android View的坐标获取方法
- React Native获得View相对于屏幕的坐标x,y
- 获取view的坐标
- 获取一个控件相对于父窗口的坐标位置
- WPF中如何获取鼠标相对于桌面的坐标
- javascript 获取图片中的一点相对于左上角的坐标
- 获取视图的绝对坐标(相对于UIWindow)
- ios 如果获取cell上控件相对于屏幕的坐标
- Android获取View坐标方法
- 获取view的坐标值
- UVa815
- codevs 舞会邀请
- 7个去伪存真的JavaScript面试题
- 深入理解Spring系列之二:BeanDefinition解析
- Fxx and game(bc上的题)
- android view相对于根布局的坐标获取
- iOS/OS X中的高精确度定时器
- 友链——Pray2018
- 蓝桥网 算法训练 最大的算式
- ELK安装
- java类和对象的创建
- Android中的parcelable接口使用
- C语言中求最大公约数以及求最小公倍数
- bzoj4197 [NOI2015] [状压dp] 寿司晚宴