Android 获取View的位置参数:x,y和相对父容器的偏移量以及中心点坐标

    • Android 屏幕坐标系
    • View中关于X,Y坐标的方法
    • 获取View的中心点坐标
    • 检测View是否绘制成功
    • 感谢观看

你好!

Android 屏幕坐标系

我们都知道,Android的View的位置可以说是在一个看不见的平面直角坐标系上摆放着:
然后,屏幕的左上角,是(0,0),然后往右依次递加X,往下依次递加Y
Android 获取View的位置参数:x,y和相对父容器的偏移量以及中心点坐标_第1张图片

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

更多相关文章

  1. android 获取原生gps坐标
  2. Android View回顾之坐标系
  3. RotateAnimation 设置旋转中心点、不停顿
  4. Android中的两种坐标系
  5. Android坐标系
  6. 根据百度地图API得到坐标和地址并在地图上显示
  7. Android 教你如何通过 LocationManager 获取得到当前位置坐标
  8. Android View坐标getLeft, getRight, getTop, getBottom

随机推荐

  1. 关于 ExpandableListView 的列子
  2. Android事件拦截处理机制理解:
  3. [置顶] android 手机 华为c8650e 无法在m
  4. 大量Android面试题目来袭
  5. Lync 2010 手机客户端登录体验
  6. Android 嵌入 LuaJIT 的曲折道路
  7. android webView 内核对比
  8. Websense:Android的安全性令人担忧
  9. 开发成本谁更高:Android PK iOS
  10. Android突破iPhone 必解兼容性困局