MotionEvent 事件解读

Android在MotionEvent里定义了一系列的手势事件

其中包括:

MotionEvent.ACTION_DOWN:当屏幕检测到第一个触点按下之后就会触发到这个事件。

MotionEvent.ACTION_MOVE:当触点在屏幕上移动时触发,触点在屏幕上停留也是会触发的,主要是由于它的灵敏度很高,而我们的手指又不可能完全静止(即使我们感觉不到移动,但其实我们的手指也在不停地抖动)。

MotionEvent.ACTION_POINTER_DOWN:当屏幕上已经有触点处于按下的状态的时候,再有新的触点被按下时触发。

MotionEvent.ACTION_POINTER_UP:当屏幕上有多个点被按住,松开其中一个点时触发(即非最后一个点被放开时)触发。

MotionEvent.ACTION_UP:当最后一个触点松开时被触发。

MotionEvent.ACTION_SCROLL:非触摸滚动,主要是由鼠标、滚轮、轨迹球触发。

MotionEvent.ACTION_CANCEL:不是由用户直接触发,有系统再需要的时候触发,例如当父view通过使函数onInterceptTouchEvent()返回true,从子view拿回处理事件的控制权是,就会给子view发一个ACTION_CANCEL事件,这里了view就再也不会收到事件了。可以将其视为ACTION_UP事件对待。

onInterceptTouchEvent()函数与onTouchEvent()的区别:

1、onInterceptTouchEvent()是用于处理事件(类似于预处理,当然也可以不处理)并改变事件的传递方向,也就是决定是否允许Touch事件继续向下(子view)传递,一但返回True(代表事件在当前的viewGroup中会被处理),则向下传递之路被截断(所有子view将没有机会参与Touch事件),同时把事件传递给当前的view的onTouchEvent()处理;返回false,则把事件交给子view的onInterceptTouchEvent()

2、onTouchEvent()用于处理事件,返回值决定当前view是否消费(consume)了这个事件,也就是说在当前view在处理完Touch事件后,是否还允许Touch事件继续向上(父view)传递,一但返回True,则父view不用操心自己来处理Touch事件。返回true,则向上传递给父view(注:可能你会觉得是否消费了有关系吗,反正我已经针对事件编写了处理代码?答案是有区别!比如ACTION_MOVE或者ACTION_UP发生的前提是一定曾经发生了ACTION_DOWN,如果你没有消费ACTION_DOWN,那么系统会认为ACTION_DOWN没有发生过,所以ACTION_MOVE或者ACTION_UP就不能被捕获。)

英文参考:

An ACTION_CANCEL happens when a parent view takes over control of one of its children views.

Take a look at the documentation around ViewGroup.onInterceptTouchEvent(MotionEvent) method. From the link:
1. You will receive the down event here.
2. The down event will be handled either by a child of this view group, or given to your own onTouchEvent() method to handle; this means you should implement onTouchEvent() to return true, so you will continue to see the rest of the gesture (instead of looking for a parent view to handle it). Also, by returning true from onTouchEvent(), you will not receive any following events in onInterceptTouchEvent() and all touch processing must happen in onTouchEvent() like normal.
3. For as long as you return false from this function, each following event (up to and including the final up) will be delivered first here and then to the target’s onTouchEvent().
4. If you return true from here, you will not receive any following events: the target view will receive the same event but with the action ACTION_CANCEL, and all further events will be delivered to your onTouchEvent() method and no longer appear here

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值