潜水已久,看了CSDN上很多大牛的博客,学了不少东西,很钦佩这种无私奉献共享的精神。
自己平时有些笔记的习惯,虽然都是一些很基础的东西,最近安定了下来,整理一下复习复习,毕竟自己一个马大哈来着。。。各种健忘
滑动一个View,改变其当前所处的位置,这个在我们APP开发中实现常用的,例如侧滑菜单,购物车的拖动等等,所以学习一下是非常有必要的
CSDN客户端上也有一个侧滑的效果
在学习如何实现滑动之前,首先要对Android中窗口坐标体系有一个大概的了解
Android坐标系:
这个没什么好说的,已屏幕最左上角的顶点作为Android坐标系的原点,往右是X轴的正方向,往下是Y轴的正方向
视图坐标系:
描述了子View与父容器之前的位置关系,在这里,原点就不再是屏幕的最上角了,而是父视图左上角为原点
MotionEvent对象:封装了触控事件中一些事件常量和常用的坐标方法
事件常量:
ACTION_DOWN 单点按下动作
ACTION_UP 单点离开动作
ACTION_MOVE 触摸移动动作
ACTION_ACTION_OUTSIDE 触摸动作超出边界
ACTION_POINTER_DOWN 多点触摸按下动作
ACTION_CANCEL 触摸动作取消
ACTION_POINTER_UP 多点离开动作
此外,还提供了很多方法来获取相应的坐标值,基本上就是通过这些坐标值来实现滑动的效果
为了防止遗忘也便于以后查询,画了个图来加深下记忆
View提供获取坐标的方法:
getTop:View 顶部 到父容器 定边的距离
getLeft:View 左边 到父容器 左边的距离
getRight:View 右边 到父容器 左边的距离(将参考点想成父容器的左边,即父容器到该View右边的距离)
etBottom:View 底部 到父容器 顶边的距离
MotionEvent提供的方法: