onInterceptTouchEvent 与 onTouchEvent 分析与MotionEvent在ViewGroup与View中的分发
onInterceptTouchEvent 与 onTouchEvent 分析与MotionEvent在ViewGroup与View中的分发
这里我还是统一给出答案吧。我们都知道假设我们不希望我们的ViewGroup阻拦我们的View获得MotionEvent。我们一般仅仅须要在onInterceptTouchEvent这个函数中return
false(而且该函数默认return false)。或者,在特定的情况下,假设我们希望某些时候交给我们的我们ViewGroup来处理有些情况下我们又希望我们的View来处理MotionEvent。这样的情况下,我们应该怎么来处理呢。我想大家都知道,我们须要在onInterceptTouchEvent做一些处理,由于大家都知道onInterceptTouchEvent是用来做MotionEvent事件的处理对象推断的。
二、通过上一篇博文我们知道dispatchTouchEvent > onTouch > onTouchEvent > onClick
这里我还是简单啰嗦一下吧。事实上onTouchListener 和 onClickListener 仅仅是为了提供给上层便捷的处理接口,他们的存在仅仅是为了对于开发提供了便捷,可是在Android中我们通常採用onInterceptTouchEvent和onTouchEvent
联合来推断MotionEvent的处理对象
三、我们在ViewGroup中採用onInterceptTouchEvent和onTouchEvent
联合来推断MotionEvent的处理对象
OK。结论到此结束,我们应该提出问题。
一、既然MotionEvent事件是由dispatchTouchEvent来分发的。那么他和onInterceptTouchEvent又是什么关系呢
是如何来完毕事件分发的
而是实实在在的重写了全部逻辑(至于View源代码的dispatchTouchEvent,各位看官去我的上一篇博文里面去看看吧)
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (mInputEventConsistencyVerifier != null) {
mInputEventConsistencyVerifier.onTouchEvent(ev, 1);
} if (DBG_MOTION || DBG_TOUCH) {
Xlog.d(TAG, "(ViewGroup)dispatchTouchEvent 1: ev = " + ev + ",mFirstTouchTarget = "
+ mFirstTouchTarget + ",this = " + this);
} boolean handled = false;
if (onFilterTouchEventForSecurity(ev)) { //上一篇博文中提到的遮蔽推断事实上是错误的,这里指的应该是窗体被其它窗体遮挡。非常明显普通情况下是不会的
final int action = ev.getAction();
final int actionMasked = action & MotionEvent.ACTION_MASK; // Handle an initial down.
if (actionMasked == MotionEvent.ACTION_DOWN) {
// Throw away all previous state when starting a new touch gesture.
// The framework may have dropped the up or cancel event for the previous gesture
// due to an app switch, ANR, or some other state change.
cancelAndClearTouchTargets(ev);
resetTouchState();
} // Check for interception.
final boolean intercepted;
if (actionMasked == MotionEvent.ACTION_DOWN
|| mFirstTouchTarget != null) {
final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0;
if (!disallowIntercept) {
intercepted = onInterceptTouchEvent(ev);//这里開始推断
/// M : add log to help debugging
if (intercepted == true) {
if (DBG_TOUCH) {
Xlog.d(TAG, "Touch event was intercepted event = " + ev + ",this = " + this);
}
}
ev.setAction(action); // restore action in case it was changed
} else {
intercepted = false;
}
} else {
// There are no touch targets and this action is not an initial down
// so this view group continues to intercept touches.
intercepted = true;
} // Check for cancelation.
final boolean canceled = resetCancelNextUpFlag(this)
|| actionMasked == MotionEvent.ACTION_CANCEL; // Update list of touch targets for pointer down, if needed.
final boolean split = (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) != 0;
if (DBG_MOTION) {
Xlog.d(TAG, "(ViewGroup)dispatchTouchEvent 2: actionMasked = " + actionMasked
+ ",intercepted = " + intercepted + ",canceled = " + canceled + ",split = "
+ split + ",mChildrenCount = " + mChildrenCount + ",mFirstTouchTarget = "
+ mFirstTouchTarget + ",this = " + this);
} TouchTarget newTouchTarget = null;
boolean alreadyDispatchedToNewTouchTarget = false;
if (!canceled && !intercepted) { //根据interceptd 分析转折点
if (actionMasked == MotionEvent.ACTION_DOWN
|| (split && actionMasked == MotionEvent.ACTION_POINTER_DOWN)
|| actionMasked == MotionEvent.ACTION_HOVER_MOVE) {
final int actionIndex = ev.getActionIndex(); // always 0 for down
final int idBitsToAssign = split ? 1 << ev.getPointerId(actionIndex)
: TouchTarget.ALL_POINTER_IDS; // Clean up earlier touch targets for this pointer id in case they
// have become out of sync.
removePointersFromTouchTargets(idBitsToAssign); final int childrenCount = mChildrenCount;
if (newTouchTarget == null && childrenCount != 0) {
final float x = ev.getX(actionIndex);
final float y = ev.getY(actionIndex);
// Find a child that can receive the event.
// Scan children from front to back.
final View[] children = mChildren; final boolean customOrder = isChildrenDrawingOrderEnabled();
for (int i = childrenCount - 1; i >= 0; i--) {
final int childIndex = customOrder ?
getChildDrawingOrder(childrenCount, i) : i;
final View child = children[childIndex];
if (!canViewReceivePointerEvents(child)
|| !isTransformedTouchPointInView(x, y, child, null)) {
if (DBG_MOTION) {
Xlog.d(TAG, "(ViewGroup)dispatchTouchEvent continue 6: i = "
+ i + ",count = " + childrenCount + ",child = " + child
+ ",this = " + this);
}
continue;
} newTouchTarget = getTouchTarget(child);
if (DBG_MOTION) {
Xlog.d(TAG, "(ViewGroup)dispatchTouchEvent to child 3: child = "
+ child + ",childrenCount = " + childrenCount + ",i = " + i
+ ",newTouchTarget = " + newTouchTarget + ",idBitsToAssign = "
+ idBitsToAssign + ",mFirstTouchTarget = " + mFirstTouchTarget
+ ",this = " + this);
}
if (newTouchTarget != null) {
// Child is already receiving touch within its bounds.
// Give it the new pointer in addition to the ones it is handling.
newTouchTarget.pointerIdBits |= idBitsToAssign;
break;
} resetCancelNextUpFlag(child);
if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) { // -------
// Child wants to receive touch within its bounds.
mLastTouchDownTime = ev.getDownTime();
mLastTouchDownIndex = childIndex;
mLastTouchDownX = ev.getX();
mLastTouchDownY = ev.getY();
newTouchTarget = addTouchTarget(child, idBitsToAssign);
alreadyDispatchedToNewTouchTarget = true;
break;
}
}
} if (newTouchTarget == null && mFirstTouchTarget != null) {
// Did not find a child to receive the event.
// Assign the pointer to the least recently added target.
newTouchTarget = mFirstTouchTarget;
while (newTouchTarget.next != null) {
newTouchTarget = newTouchTarget.next;
}
newTouchTarget.pointerIdBits |= idBitsToAssign;
}
}
} // Dispatch to touch targets.
if (mFirstTouchTarget == null) {
if (DBG_MOTION) {
Xlog.d(TAG, "(ViewGroup)dispatchTouchEvent mFirstTouchTarget = null, canceled = "
+ canceled + ",this = " + this);
}
// No touch targets so treat this as an ordinary view.
handled = dispatchTransformedTouchEvent(ev, canceled, null, //--------
TouchTarget.ALL_POINTER_IDS);
} else {
// Dispatch to touch targets, excluding the new touch target if we already
// dispatched to it. Cancel touch targets if necessary.
TouchTarget predecessor = null;
TouchTarget target = mFirstTouchTarget;
while (target != null) {
final TouchTarget next = target.next;
if (alreadyDispatchedToNewTouchTarget && target == newTouchTarget) {
handled = true;
} else {
final boolean cancelChild = resetCancelNextUpFlag(target.child)
|| intercepted;
if (dispatchTransformedTouchEvent(ev, cancelChild, //----------
target.child, target.pointerIdBits)) {
handled = true;
}
if (DBG_MOTION) {
Xlog.d(TAG, "dispatchTouchEvent middle 5: cancelChild = " + cancelChild
+ ",mFirstTouchTarget = " + mFirstTouchTarget + ",target = "
+ target + ",predecessor = " + predecessor + ",next = " + next
+ ",this = " + this);
}
if (cancelChild) {
if (predecessor == null) {
mFirstTouchTarget = next;
} else {
predecessor.next = next;
}
target.recycle();
target = next;
continue;
}
}
predecessor = target;
target = next;
}
} // Update list of touch targets for pointer up or cancel, if needed.
if (canceled
|| actionMasked == MotionEvent.ACTION_UP
|| actionMasked == MotionEvent.ACTION_HOVER_MOVE) {
resetTouchState();
} else if (split && actionMasked == MotionEvent.ACTION_POINTER_UP) {
final int actionIndex = ev.getActionIndex();
final int idBitsToRemove = 1 << ev.getPointerId(actionIndex);
removePointersFromTouchTargets(idBitsToRemove);
}
} if (DBG_MOTION) {
Xlog.d(TAG, "(ViewGroup)dispatchTouchEvent end 4: handled = " + handled + ",mFirstTouchTarget = "
+ mFirstTouchTarget + ",this = " + this);
} if (!handled && mInputEventConsistencyVerifier != null) {
mInputEventConsistencyVerifier.onUnhandledEvent(ev, 1);
}
return handled;
}
private boolean dispatchTransformedTouchEvent(MotionEvent event, boolean cancel,
View child, int desiredPointerIdBits) {
final boolean handled; // Canceling motions is a special case. We don't need to perform any transformations
// or filtering. The important part is the action, not the contents.
final int oldAction = event.getAction();
if (DBG_MOTION) {
Xlog.d(TAG, "dispatchTransformedTouchEvent 1: event = " + event + ",cancel = "
+ cancel + ",oldAction = " + oldAction + ",desiredPointerIdBits = "
+ desiredPointerIdBits + ",mFirstTouchTarget = " + mFirstTouchTarget
+ ",child = " + child + ",this = " + this);
} if (cancel || oldAction == MotionEvent.ACTION_CANCEL) {
event.setAction(MotionEvent.ACTION_CANCEL);
if (child == null) {
handled = super.dispatchTouchEvent(event);
} else {
handled = child.dispatchTouchEvent(event);
}
event.setAction(oldAction);
if (DBG_MOTION) {
Xlog.d(TAG, "Dispatch cancel action end: handled = " + handled + ",oldAction = "
+ oldAction + ",child = " + child + ",this = " + this);
}
return handled;
} // Calculate the number of pointers to deliver.
final int oldPointerIdBits = event.getPointerIdBits();
final int newPointerIdBits = oldPointerIdBits & desiredPointerIdBits; // If for some reason we ended up in an inconsistent state where it looks like we
// might produce a motion event with no pointers in it, then drop the event.
if (newPointerIdBits == 0) {
Xlog.i(TAG, "Dispatch transformed touch event without pointers in " + this);
return false;
} // If the number of pointers is the same and we don't need to perform any fancy
// irreversible transformations, then we can reuse the motion event for this
// dispatch as long as we are careful to revert any changes we make.
// Otherwise we need to make a copy.
final MotionEvent transformedEvent;
if (newPointerIdBits == oldPointerIdBits) {
if (child == null || child.hasIdentityMatrix()) {
if (child == null) {
handled = super.dispatchTouchEvent(event);
} else {
final float offsetX = mScrollX - child.mLeft;
final float offsetY = mScrollY - child.mTop;
event.offsetLocation(offsetX, offsetY); handled = child.dispatchTouchEvent(event); event.offsetLocation(-offsetX, -offsetY);
}
if (DBG_MOTION) {
Xlog.d(TAG, "dispatchTransformedTouchEvent 2 to child " + child
+ ",handled = " + handled + ",mScrollX = " + mScrollX + ",mScrollY = "
+ mScrollY + ",mFirstTouchTarget = " + mFirstTouchTarget + ",event = "
+ event + ",this = " + this);
}
return handled;
}
transformedEvent = MotionEvent.obtain(event);
} else {
transformedEvent = event.split(newPointerIdBits);
} // Perform any necessary transformations and dispatch.
if (child == null) {
handled = super.dispatchTouchEvent(transformedEvent);
} else {
final float offsetX = mScrollX - child.mLeft;
final float offsetY = mScrollY - child.mTop;
transformedEvent.offsetLocation(offsetX, offsetY);
if (! child.hasIdentityMatrix()) {
transformedEvent.transform(child.getInverseMatrix());
} handled = child.dispatchTouchEvent(transformedEvent);
} if (DBG_MOTION) {
Xlog.d(TAG, "dispatchTransformedTouchEvent 3 to child " + child + ",handled = "
+ handled + ",mScrollX = " + mScrollX + ",mScrollY = " + mScrollY
+ ",mFirstTouchTarget = " + mFirstTouchTarget + ",transformedEvent = "
+ transformedEvent + ",this = " + this);
} // Done.
transformedEvent.recycle();
return handled;
}
从上面的代码来推断我们如果我们不重载
public boolean onInterceptTouchEvent(MotionEvent ev) {
return false;
}
那么非常明显了,我们的ViewGroup不会吃掉本次MotionEvent
我们继续接着我们的Log来分析
false;所以我们的MyFrameLayout没有资格消费本次的MotionEvent事件。终于经过推断dispatchTransformedTouchEvent把事件交给了我们的MyImageView
handled = child.dispatchTouchEvent(transformedEvent);
当然
由于我们的MyImageView 也设置了onTouchListener和onClickListener,所以他顺理成章的消费了本次MotionEvent
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvamF5c29uZzIwMTI=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" style="border:none; max-width:100%">
if (child == null) {
handled = super.dispatchTouchEvent(transformedEvent);
} else
我们的MyFrameLayout 也还是终于享受到了MotionEvent
返回了false 我们的ViewGroup就不能接收到OnTouchEvent了
当我们的onInterceptTouchEvent 被我们重载 并返回了true 那么走的路线就不一样了
if (!canceled && !intercepted) { //根据interceptd 分析转折点</span>
if (mFirstTouchTarget == null) {
if (DBG_MOTION) {
Xlog.d(TAG, "(ViewGroup)dispatchTouchEvent mFirstTouchTarget = null, canceled = "
+ canceled + ",this = " + this);
}
// No touch targets so treat this as an ordinary view.
handled = dispatchTransformedTouchEvent(ev, canceled, null,
TouchTarget.ALL_POINTER_IDS);
这里注意,由于我们的onInterceptTouchEvent return true;我们直接跳过了Child的推断并终于导致我们的child == null;那么非常明星我们的MotionEvent仅仅能被我们的ViewGroup消费了。
那么,到了这里我们開始大胆的做出如果性结论:
一、仅仅要我们的onInterceptTouchEvent return
true 那么我们的MotionEvent 与ChildView 无缘
二、假设我们的onInterceptTouchEvent return false。那么我们的ChildView 会优先获得MotionEvent
。可是当我们的ChildView 并不在TouchTarget上,我们的ViewGroup依旧有机会得到本次MotionEvent 。
那么接下来,我们全部做的就仅仅能验证我们的结论了。
这里我们先验证第一条// 这里的验证我们将根据ACTION的不同来分析。
这里我们对我们的MyFrameLayout 做出改变(重载onInterceptTouchEvent )
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:{
Log.d(TAG,"MyFrameLayout onInterceptTouchEvent ACTION_DOWN return true");
return true;
}
case MotionEvent.ACTION_MOVE:{
Log.d(TAG,"MyFrameLayout onInterceptTouchEvent ACTION_MOVE return true");
return true;
}
case MotionEvent.ACTION_UP:{
Log.d(TAG,"MyFrameLayout onInterceptTouchEvent ACTION_UP return true");
return true;
}
}
return true;
}
简而言之。就是任何情况下,我们都要我们的onInterceptTouchEvent return true;
和ImageView的OnTouchEvent函数
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.d(TAG,"MyFrameLayout onTouchEvent"+event.getAction());
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:{
Log.d(TAG,"MyFrameLayout onTouchEvent ACTION_DOWN return true");
break;
}
case MotionEvent.ACTION_MOVE:{
Log.d(TAG,"MyFrameLayout onTouchEvent ACTION_MOVE return true");
break;
}
case MotionEvent.ACTION_UP:{
Log.d(TAG,"MyFrameLayout onTouchEvent ACTION_UP return true");
break;
}
}
return super.onTouchEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.d(TAG,"MyImageView onTouchEvent"+event.getAction());
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:{
Log.d(TAG,"MyImageView onTouchEvent ACTION_DOWN return true");
break;
}
case MotionEvent.ACTION_MOVE:{
Log.d(TAG,"MyImageView onTouchEvent ACTION_MOVE return true");
break;
}
case MotionEvent.ACTION_UP:{
Log.d(TAG,"MyImageView onTouchEvent ACTION_UP return true");
break;
}
}
return super.onTouchEvent(event);
}
首先我们点击我们的MyImageView,我们来看一下Log
return true,我们的子View就与MotionEvent 无缘了
中的MotionEvent.ACTION_DOWN returnfalse。
returnfalse;所以我们的FrameLayout没办法继续往下走了(不会走onTouch 和 onTouchEvent)转而MotionEvent事件转交给我们的ImageView
返回了true。所以非常明显我们的FrameLayout告诉了系统 这次的MotionEvent事件我要了。所以我们的ImageView的到了MotionEvent.ACTION_CANCEL
反而我们的FrameLayout 能够继续往下走了。
可是没有ACTION_DOWN 也是徒劳。
相同不会OnClickListener
假设我们的FrameLayout在onInterceptTouchEvent 在ACTION_MOVE中返回false,我们的ImageView能够接受到ACTION_MOVE
可是终于接受的还是ACTION_CANCEL依旧接受不到ACTION_UP ,反而我们的FrameLayout
仅仅能得到ACTION_UP 。简而言之就是说根据onInterceptTouchEvent 的返回值不同各MotionEvent事件终于要么被ViewGroup
要么被ChildView获取
并不会在每一次 MotionEvent事件(ACTION_DOWN、ACTION_MOVE、ACTION_UP
等)调用,例假设在ACTION_DOWN return true 交给了ViewGroup 而View没有得到的话。ACTION_MOVE时就不会调用,可是假设return
false,ChildView 得到了ACTION_MOVE时就会再次调用
onInterceptTouchEvent 与 onTouchEvent 分析与MotionEvent在ViewGroup与View中的分发的更多相关文章
- Android中的dispatchTouchEvent()、onInterceptTouchEvent()和onTouchEvent()
dispatchTouchEvent (分发TouchEvent) 处理触摸事件分发,事件(多数情况)是从Activity的dispatchTouchEvent开始的.执行super.dispatc ...
- Android进阶——Android事件分发机制之dispatchTouchEvent、onInterceptTouchEvent、onTouchEvent
Android事件分发机制可以说是我们Android工程师面试题中的必考题,弄懂它的原理是我们避不开的任务,所以长痛不如短痛,花点时间干掉他,废话不多说,开车啦 Android事件分发机制的发生在Vi ...
- Android:ViewGroup和View的Touch事件
Android中ViewGroup和View中的Touch事件传递机制分析 关键字:GroupView:View:Touch事件 基础知识: onInterceptTouchEvent():在View ...
- onInterceptTouchEvent和onTouchEvent举例分析
首先自定义三个组件,其关系是:MyLayout在最上面,MySubLayout在MyLayout下面,MyView在MySubLayout下面. 一个点击事件进来,首先是DOWN动作,先是MyLayo ...
- onInterceptTouchEvent和onTouchEvent调用时序
onInterceptTouchEvent()是ViewGroup的一个方法,目的是在系统向该ViewGroup及其各个childView触发onTouchEvent()之前对相关事件进行一次拦截,A ...
- Android Touch系统简介(二):实例详解onInterceptTouchEvent与onTouchEvent的调用过程
上一篇文章主要讲述了Android的TouchEvent的分发过程,其中有两个重要的函数:onInterceptTouchEvent和onTouchEvent,这两个函数可被重装以完成特定的逻辑.on ...
- Android Touch系统简介(二):实例详解onInterceptTouchEvent与onTouchEvent的调用过程
上一篇文章主要讲述了Android的TouchEvent的分发过程,其中有两个重要的函数:onInterceptTouchEvent和onTouchEvent,这两个函数可被重装以完成特定的逻辑.on ...
- Android在onInterceptTouchEvent与onTouchEvent
onInterceptTouchEvent: onInterceptTouchEvent是在ViewGroup里面定义的.Android中的layout布局类一般都是继承此类的.onIntercept ...
- onInterceptTouchEvent和onTouchEvent调用关系详解 ...
http://blog.csdn.net/lvxiangan/article/details/9309927 老实说,这两个小东东实在是太麻烦了,很不好懂,我自己那api文档都头晕,在网上找到很多资料 ...
随机推荐
- 使用html+css+js实现3D相册
使用html+css+js实现3D相册,快来上传的照片吧 效果图: 代码如下,复制即可用: <!DOCTYPE html> <html lang="en"> ...
- 【笔记】jQuery插件开发指南
原文链接:http://www.cnblogs.com/Wayou/p/jquery_plugin_tutorial.html (有部分增删和修改) jQuery插件开发模式 软件开发过程中是需要一定 ...
- Mysql建表语句
create table messages( id int unsigned primary key auto_increment, sender varchar(64) not null, gett ...
- centos 6.x 部署uwsgi+flask项目
一.项目背景 1. 公司需求要做一个在线统计页面; 2. 统计在线人数,进行人数数据展示; 3. 类似QQ官网在线人数 二.测试环境 [root@linux-node2 ~]# cat /etc/*r ...
- Http PipeLining
Http PipeLining */--> div.org-src-container { font-size: 85%; font-family: monospace; } pre.src { ...
- 2015309南皓芯《Java程序设计》实验一(Java开发环境的熟悉)实验报告
一.实验内容及步骤 (一)使用JDK编译.运行简单的java程序 命令行下的程序开发 步骤一(新建文件夹):打开windows下的cmd → 输入cd Code命令进入Code目录 → 输入md 20 ...
- redux-saga印象
saga作为redux的中间件,用来处理异步任务. 先收集资料: 贴个文章(2)中的图先: 注意:参考文献(4)是redux-saga作者写的. 参考文章: (1)https://redux-saga ...
- CF475C. Kamal-ol-molk's Painting
C. Kamal-ol-molk's Painting time limit per test 2 seconds memory limit per test 256 megabytes input ...
- Observer 观察者
意图 定义对象间的一种一对多的依赖关系 ,当一个对象的状态发生改变时, 所有依赖于它的对象都得到通知并被自动更新. 动机 一致性,松耦合 需要维护相关对象间的一致性.我们不希望为了维持一致性而使各类紧 ...
- 2014-2015 ACM-ICPC, NEERC, Moscow Subregional Contest B - Bring Your Own Bombs 离散化+扫描线+计算期望
扫描线一边扫一边算期望,细节比较多. #include<bits/stdc++.h> #define LL long long #define fi first #define se se ...