Android自定义View基础——坐标系
2017-06-06 11:43
225 查看
参考文章:
http://www.gcssloop.com/customview/CoordinateSystem
http://blog.csdn.net/lvxiangan/article/details/19971509
http://blog.csdn.net/yanbober/article/details/50419117/
![](https://img-blog.csdn.net/20170606104752146?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMjg1OTU2Nzk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
相应方法的解释:
1、View的静态方法(不考虑View的平移操作):
getLeft(): 返回View自身左边到父布局左边的距离
getTop() 返回View自身顶边到父布局顶边的距离
getRight() 返回View自身右边到父布局左边的距离
getBottom() 返回View自身底边到父布局顶边的距离
[b]需要注意的是,这四个方法获取的坐标表示的是View原始状态时相对于父容器的坐标,对View进行平移操作并不会改变这四个方法的返回值。[/b]
一: getX()与getY()方法获取的是View左上角相对于父容器的坐标,当View没有发生平移操作时,getX()==getLeft()、getY==getTop()。
二: translationX与 translationY是View左上角相对于父容器的偏移量:translationX = getX() - getLeft(),当View未发生平移操作时,translationX 与translationY都为0。
2、MotionEvent
当重写View的onTouchEvent(MotionEvent event)方法时Android会把触摸事件封装成MotionEvent成提供给开发者,MotionEvent提供了两组用于获取用户点击点坐标的方法:
getX() 当前触摸事件距离当前View左边的距离
getY() 当前触摸事件距离当前View顶边的距离
getRawX() 当前触摸事件距离整个屏幕左边的距离
getRawY() 当前触摸事件距离整个屏幕顶边的距离
http://www.gcssloop.com/customview/CoordinateSystem
http://blog.csdn.net/lvxiangan/article/details/19971509
http://blog.csdn.net/yanbober/article/details/50419117/
安卓屏幕的坐标系:
原点是左上角(0,0),向下是y轴正方向,向右是x轴正方向相应方法的解释:
1、View的静态方法(不考虑View的平移操作):
getLeft(): 返回View自身左边到父布局左边的距离
getTop() 返回View自身顶边到父布局顶边的距离
getRight() 返回View自身右边到父布局左边的距离
getBottom() 返回View自身底边到父布局顶边的距离
[b]需要注意的是,这四个方法获取的坐标表示的是View原始状态时相对于父容器的坐标,对View进行平移操作并不会改变这四个方法的返回值。[/b]
一: getX()与getY()方法获取的是View左上角相对于父容器的坐标,当View没有发生平移操作时,getX()==getLeft()、getY==getTop()。
二: translationX与 translationY是View左上角相对于父容器的偏移量:translationX = getX() - getLeft(),当View未发生平移操作时,translationX 与translationY都为0。
2、MotionEvent
当重写View的onTouchEvent(MotionEvent event)方法时Android会把触摸事件封装成MotionEvent成提供给开发者,MotionEvent提供了两组用于获取用户点击点坐标的方法:
getX() 当前触摸事件距离当前View左边的距离
getY() 当前触摸事件距离当前View顶边的距离
getRawX() 当前触摸事件距离整个屏幕左边的距离
getRawY() 当前触摸事件距离整个屏幕顶边的距离
相关文章推荐
- Android自定义View基础(一)-坐标系
- android自定义View基础之------android坐标系
- Android自定义View之旅(基础篇1:坐标系)
- Android自定义View(一)__坐标系
- Android自定义View基础(二)-角度与弧度
- Android 自定义View基础-View的绘制
- android自定义View一(基础和原理)
- Android 自定义View(Canvas和Path)绘制基础图形(二)
- Android自定义View基础学习
- Android自定义View-坐标系
- Android 从0开始自定义控件之 自定义 View 基础实例(十)
- [自定义控件]android自定义view基础
- android 自定义view基础知识
- Android 自定义View基础(六)--ViewDragHelper(侧滑栏)
- android自定义View基础系列一(点击随机生成验证码效果)
- android 自定义View(一、基础认识)
- Android的自定义View的坐标系
- 安卓自定义View基础-坐标系
- android自定义View之零散基础知识
- Android 自定义view基础(二)