View坐标系详解(getTop(),getLeft(),getX(),getY(),getLocationOnScreen(), getLocationInWindow())
2017-11-23 14:40
483 查看
View 提供了如下 5 种方法获取 View 的坐标:
1. View.getTop()、View.getLeft()、View.getBottom()、View.getRight();
2. View.getX()、View.getY();
3. View.getTranslationX()、View.getTranslationY();
4. View.getLocationOnScreen(int[] position);
5. View.getLocationInWindow(int[] position);
初学View时见到这个多个方法可能有点懵逼,下面我来根据编号一一介绍:
**1.** 这四个方法用于获取View相对于父容器的坐标,如下图
从上图可知,可根据View的这四个坐标来获取View的大小:
需要注意的是,这四个方法获取的坐标表示的是View原始状态时相对于父容器的坐标,对View进行平移操作并不会改变着四个方法的返回值。
2. getX()与getY()方法获取的是View左上角相对于父容器的坐标,当View没有发生平移操作时,getX()==getLeft()、getY==getTop()。
3. translationX与 translationY是View左上角相对于父容器的偏移量:translationX = getX() - getLeft(),当View未发生平移操作时,translationX 与translationY都为0。
4. 获取View相对于整个屏幕的坐标。
5. 获取View相对于Window的坐标(忽略状态栏及ActionBar)。
上面介绍的是View获取坐标的几种方法,但是在实际开发中不仅需要获取View的坐标,有时候还需要获取用户点击时的坐标。
getX()和getY()获取到的是相对于当前View左上角的坐标; getRawX和getRawY()获取的是相对于屏幕左上角的坐标。 如下图(画的不好,将就着看吧:-D):
1. View.getTop()、View.getLeft()、View.getBottom()、View.getRight();
2. View.getX()、View.getY();
3. View.getTranslationX()、View.getTranslationY();
4. View.getLocationOnScreen(int[] position);
5. View.getLocationInWindow(int[] position);
初学View时见到这个多个方法可能有点懵逼,下面我来根据编号一一介绍:
**1.** 这四个方法用于获取View相对于父容器的坐标,如下图
从上图可知,可根据View的这四个坐标来获取View的大小:
View的Height值 = view.getBottom() - view.getTop(); View的Width值 = view.getRight() - view.getLeft();
需要注意的是,这四个方法获取的坐标表示的是View原始状态时相对于父容器的坐标,对View进行平移操作并不会改变着四个方法的返回值。
2. getX()与getY()方法获取的是View左上角相对于父容器的坐标,当View没有发生平移操作时,getX()==getLeft()、getY==getTop()。
3. translationX与 translationY是View左上角相对于父容器的偏移量:translationX = getX() - getLeft(),当View未发生平移操作时,translationX 与translationY都为0。
4. 获取View相对于整个屏幕的坐标。
5. 获取View相对于Window的坐标(忽略状态栏及ActionBar)。
上面介绍的是View获取坐标的几种方法,但是在实际开发中不仅需要获取View的坐标,有时候还需要获取用户点击时的坐标。
MotionEvent
当重写View的onTouchEvent(MotionEvent event)方法时android会把触摸事件封装成MotionEvent成提供给开发者,MotionEvent提供了两组用于获取用户点击点坐标的方法:1. getX()、getY(); 2. getRawX、getRawY();
getX()和getY()获取到的是相对于当前View左上角的坐标; getRawX和getRawY()获取的是相对于屏幕左上角的坐标。 如下图(画的不好,将就着看吧:-D):
相关文章推荐
- Android View api 详解-LocationInWindow,getLocationOnScreen,getGlobalVisibleRect,getLocalVisibleRect
- getLocationOnScreen; View.getLocationInWindow(int[] location)
- Android ListView在增加HeaderView之后使用getLocationInWindow和getLocationOnScreen获得值不正确的解决方法
- View getLocationInWindow getLocationOnScreen
- Android坐标getLeft,getRight,getTop,getBottom,getLocationInWindow和getLocationOnScreen
- View在setScale后,getTop,getLeft和getLocationOnScreen的区别
- 使用getLocationInWindow或getLocationOnScreen获得View在屏幕中的坐标
- Android坐标getLeft,getRight,getTop,getBottom,getLocationInWindow和getLocationOnScreen
- android中getLocationInWindow 和 getLocationOnScreen的区别
- android中getLocationInWindow 和 getLocationOnScreen的区别
- getLocationOnScreen vs getLocationInWindow
- Android View坐标系详解(getTop()、getX、getTranslationX...)
- Android中getLocationOnScreen和getLocationInWindow 获取屏幕大小
- getLocationInWindow()与getLocationOnScreen()
- android获得控件在屏幕中的绝对坐标 getLocationInWindow 和 getLocationOnScreen
- android获得控件在屏幕中的绝对坐标 getLocationInWindow 和 getLocationOnScreen
- android获得控件在屏幕中的绝对坐标 getLocationInWindow 和 getLocationOnScreen
- android获得控件在屏幕中的绝对坐标 getLocationInWindow 和 getLocationOnScreen
- android中getLocationInWindow和getLocationOnScreen的区别
- android获得控件在屏幕中的绝对坐标 getLocationInWindow 和 getLocationOnScreen