标签:
这里我还是统一给出答案吧。我们都知道假设我们不希望我们的ViewGroup阻拦我们的View获得MotionEvent。我们一般仅仅须要在onInterceptTouchEvent这个函数中return false(而且该函数默认return false)。或者,在特定的情况下,假设我们希望某些时候交给我们的我们ViewGroup来处理有些情况下我们又希望我们的View来处理MotionEvent。这样的情况下,我们应该怎么来处理呢。我想大家都知道,我们须要在onInterceptTouchEvent做一些处理,由于大家都知道onInterceptTouchEvent是用来做MotionEvent事件的处理对象推断的。
而是实实在在的重写了全部逻辑(至于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;
}handled = child.dispatchTouchEvent(transformedEvent);当然 由于我们的MyImageView 也设置了onTouchListener和onClickListener,所以他顺理成章的消费了本次MotionEvent
if (child == null) {
handled = super.dispatchTouchEvent(transformedEvent);
} else 我们的MyFrameLayout 也还是终于享受到了MotionEventif (!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);@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;
}
@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);
}
相同不会OnClickListener

onInterceptTouchEvent 与 onTouchEvent 分析与MotionEvent在ViewGroup与View中的分发
标签:
原文地址:http://www.cnblogs.com/bhlsheji/p/5379100.html