Android(安卓)获取View的位置参数:x,y和相对父容器的偏移量以及中心点坐标
Android 获取View的位置参数:x,y和相对父容器的偏移量以及中心点坐标
- Android 屏幕坐标系
- View中关于X,Y坐标的方法
- 获取View的中心点坐标
- 检测View是否绘制成功
- 感谢观看
你好!
Android 屏幕坐标系
我们都知道,Android的View的位置可以说是在一个看不见的平面直角坐标系上摆放着:
然后,屏幕的左上角,是(0,0),然后往右依次递加X,往下依次递加Y
View中关于X,Y坐标的方法
The horizontal location of this view relative to its left position. This position is post-layout, in addition to wherever the object’s layout placed it.
getTranslationX() // View左上角相对于父容器的横向偏移量 public float getTranslationX ()
The vertical location of this view relative to its top position. This position is post-layout, in addition to wherever the object’s layout placed it.
getTranslationY() //View左上角相对于父容器纵向偏移量 public float getTranslationY ()
Top position of this view relative to its parent.
getTop () //View原始左上角的x坐标的值,不管View怎么移动,这个值不会 发生改变 public final int getTop ()
Left position of this view relative to its parent.
getLeft() //View原始左上角Y坐标的值,同上,也是不会改变 public final int getLeft ()
Bottom position of this view relative to its parent.
getBottom() // View原始右下角Y坐标的值,同上,也是不会改变 public final int getBottom ()
Right position of this view relative to its parent.
getRight() //View原始右下角X坐标的值,同上,也是不会发生改变 public final int getRight ()
The visual x position of this view, in pixels. This is equivalent to the translationX property plus the current left property.
getX() //获取View左上角的X坐标,相当于getLeft()+getTranslationX()public float getX ()
The visual y position of this view, in pixels. This is equivalent to the translationY property plus the current top property.
getY() //获取View左上角的Y坐标,相当于getTop+getTranslationY() public float getY()
The x location of the point around which the view is rotated and scaled.
getPivotX()//view围绕其旋转和缩放的点的X位置 public float getPivotX ()
The y location of the point around which the view is rotated and scaled.
getPivotY() //view围绕其旋转和缩放的点的Y位置public float getPivotY ()
获取View的中心点坐标
如果想要获取View中心点的坐标,首先你需要知道它某个角的坐标和他的长和宽, 所以,我们首先要获取View一个角的坐标,
//左上角坐标 (left,top) int top = view.getTop(); int left = view.getLeft();
然后,我们还需要知道View的长和宽,想要知道View的 长和宽,除了测量,这里还有一个办法就是拿右下角的横坐标,减去左上角的横坐标,就得到了View的宽,拿右下角的纵坐标,减去左上角的纵坐标,就得到了View的高
//右下角坐标 int right = view.getRight(); int bottom = view.getBottom();//View的 宽int viewWidth = right - left;//View的 高int viewHeight = bottom - top;
在之后,我们知道了View的左上角坐标以及View的长和宽,那么,View的中心点坐标就可以用 左上角的x加上View的宽除以2,和左上角的Y加上View的高除以2
//中心点的Y坐标 int centerY = top+viewHeight/2; //中心点的X坐标 int centerX = left+viewWidth/2;
检测View是否绘制成功
如果你已经直接在代码中获取了X,Y的坐标,你会发现,结果是(0,0),这是因为View还没有绘制成功,所以你获取他的坐标就是(0,0),所以,你需要在View绘制成功的回调内进获取坐标
//view绘制完成时回调view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { // 已经完成绘制,干你要干的事情 }});
感谢观看
我是入梦,谢谢你的观看我的博客,如果有什么错误,请随时联系我,QQ:897589417
更多相关文章
- [置顶] android滑动基础篇
- 安装Android(安卓)studio 时候显示:SDK tools directory is missi
- 第五章 Android(安卓)Scroll 分析
- 毫无保留开源我写的:IOS Android(安卓)Ipad 多点触摸通用js 库
- android 自定义Dialog背景透明及显示位置设置
- Android(安卓)获取控件宽高的3种方法
- Android之GLES2.0显示图片测试代码
- Android_自定义View拖拽重绘
- Android中贪吃蛇游戏的学习(三)