Viewのイベント処理メカニズムソース分析
8837 ワード
Viewのイベント配信は主にMotionEventイベント向けの配信であり,以下ではAndroidソースコードを用いてMotionEventイベントの配信過程を一歩一歩分析する.
クリックイベントが発生すると、イベントはまず現在のactivityに渡され、現在のactivityのdispatchTouchEventメソッドによって配布されます.次に、関数のソースコードを見てみましょう.
コードから分かるように、イベントの配布作業は主に現在のactivity内部のWindowに任せて配布され、WindowのsuperDispatchTouchEventメソッドを引き続き見る.Windowクラスの唯一の実装はPhoneWindowクラスであるため、次のステップではPhoneWindowのsuperDispatchTouchEventメソッドを引き続き表示する.コードは以下の通りである.
PhoneWindowクラスは何もしていないことがわかりますが、イベントをmDecor処理に渡すだけです.mDecorはDecorViewタイプのオブジェクトで、DecorViewはFrameLayoutから、FrameLayoutはView Groupから、次はDecorViewのsuperDispatchTouchEventメソッドを見てみましょう
親クラスのdispatchTouchEventメソッドを呼び出しただけで、FrameLayoutはメソッドを書き換えていないため、最終的にはView Groupのメソッドを呼び出していることがわかります.次に,ViewGroupにおけるイベント配信処理の過程を重点的に分析し,主にdispatchTouchEventメソッドにおけるコードフラグメントを見る.
ここのコードは、イベントがACTION_である場合、ViewGroupが現在のイベントに対してonInterceptTouchEventを呼び出すかどうかを示します.DOWNの場合は必ずonInterceptTouchEvent,mFirstTouchTargetを呼び出します!=nullが空でない場合は、ViewGroupがACTION_をDOWNがそのいずれかのサブView処理に渡された場合、そのViewのDispatchTouchEventがtrueに戻った場合(すなわち、そのViewがこのACTION_DOWNイベントを処理する)、mFirstTouchTargetがそのViewに向けられる.したがって、同一イベントシーケンスの後続イベントにおけるACTION_MOVE,...,ACTION_UPイベントが来るとonInterceptTouchEventは呼び出されません.ViewGroupがイベントをブロックしない場合、イベントはViewGroupのサブViewによって処理されます.具体的なコードは次のとおりです.
イベントをサブViewに渡すかどうかは、次の2つの関数で判断され、canViewReceivePointerEventsとi s T r a n s f ormedTouchPointInViewは、サブViewが表示されているかどうかとアニメーションが再生されているかどうかを判断し、後者はクリックイベントの座標がサブエレメントの領域内に落ちているかどうかを判断し、この2つが満たされている場合、イベントはそのサブエレメントに処理され、イベントの配布が完了する.サブエレメントのdispatchTouchEventメソッドがdispatchTransformedTouchEventで呼び出されます
dispatchTransformedTouchEventがtrueを返す場合、すなわちサブViewはこのACTION_を処理するDOWNイベントでは、次のifブランチのコードを実行します.
子ViewがACTIONを処理したらDOWNイベントでは、addTouchTargetメソッドでmFirstTouchTargetがこのサブViewに割り当てられます.コードは次のとおりです.
クリックイベントが発生すると、イベントはまず現在のactivityに渡され、現在のactivityのdispatchTouchEventメソッドによって配布されます.次に、関数のソースコードを見てみましょう.
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
onUserInteraction();
}
if (getWindow().superDispatchTouchEvent(ev)) {
return true;
}
return onTouchEvent(ev);
}
コードから分かるように、イベントの配布作業は主に現在のactivity内部のWindowに任せて配布され、WindowのsuperDispatchTouchEventメソッドを引き続き見る.Windowクラスの唯一の実装はPhoneWindowクラスであるため、次のステップではPhoneWindowのsuperDispatchTouchEventメソッドを引き続き表示する.コードは以下の通りである.
@Override
public boolean superDispatchTouchEvent(MotionEvent event) {
return mDecor.superDispatchTouchEvent(event);
}
PhoneWindowクラスは何もしていないことがわかりますが、イベントをmDecor処理に渡すだけです.mDecorはDecorViewタイプのオブジェクトで、DecorViewはFrameLayoutから、FrameLayoutはView Groupから、次はDecorViewのsuperDispatchTouchEventメソッドを見てみましょう
public boolean superDispatchTouchEvent(MotionEvent event) {
return super.dispatchTouchEvent(event);
}
親クラスのdispatchTouchEventメソッドを呼び出しただけで、FrameLayoutはメソッドを書き換えていないため、最終的にはView Groupのメソッドを呼び出していることがわかります.次に,ViewGroupにおけるイベント配信処理の過程を重点的に分析し,主にdispatchTouchEventメソッドにおけるコードフラグメントを見る.
// 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);
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;
}
ここのコードは、イベントがACTION_である場合、ViewGroupが現在のイベントに対してonInterceptTouchEventを呼び出すかどうかを示します.DOWNの場合は必ずonInterceptTouchEvent,mFirstTouchTargetを呼び出します!=nullが空でない場合は、ViewGroupがACTION_をDOWNがそのいずれかのサブView処理に渡された場合、そのViewのDispatchTouchEventがtrueに戻った場合(すなわち、そのViewがこのACTION_DOWNイベントを処理する)、mFirstTouchTargetがそのViewに向けられる.したがって、同一イベントシーケンスの後続イベントにおけるACTION_MOVE,...,ACTION_UPイベントが来るとonInterceptTouchEventは呼び出されません.ViewGroupがイベントをブロックしない場合、イベントはViewGroupのサブViewによって処理されます.具体的なコードは次のとおりです.
final View[] children = mChildren;
for (int i = childrenCount - 1; i >= 0; i--) {
final int childIndex = customOrder
? getChildDrawingOrder(childrenCount, i) : i;
final View child = (preorderedList == null)
? children[childIndex] : preorderedList.get(childIndex);
// If there is a view that has accessibility focus we want it
// to get the event first and if not handled we will perform a
// normal dispatch. We may do a double iteration but this is
// safer given the timeframe.
if (childWithAccessibilityFocus != null) {
if (childWithAccessibilityFocus != child) {
continue;
}
childWithAccessibilityFocus = null;
i = childrenCount - 1;
}
if (!canViewReceivePointerEvents(child)
|| !isTransformedTouchPointInView(x, y, child, null)) {
ev.setTargetAccessibilityFocus(false);
continue;
}
newTouchTarget = getTouchTarget(child);
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();
if (preorderedList != null) {
// childIndex points into presorted list, find original index
for (int j = 0; j < childrenCount; j++) {
if (children[childIndex] == mChildren[j]) {
mLastTouchDownIndex = j;
break;
}
}
} else {
mLastTouchDownIndex = childIndex;
}
mLastTouchDownX = ev.getX();
mLastTouchDownY = ev.getY();
newTouchTarget = addTouchTarget(child, idBitsToAssign);
alreadyDispatchedToNewTouchTarget = true;
break;
}
// The accessibility focus didn't handle the event, so clear
// the flag and do a normal dispatch to all children.
ev.setTargetAccessibilityFocus(false);
}
if (preorderedList != null) preorderedList.clear();
}
イベントをサブViewに渡すかどうかは、次の2つの関数で判断され、canViewReceivePointerEventsとi s T r a n s f ormedTouchPointInViewは、サブViewが表示されているかどうかとアニメーションが再生されているかどうかを判断し、後者はクリックイベントの座標がサブエレメントの領域内に落ちているかどうかを判断し、この2つが満たされている場合、イベントはそのサブエレメントに処理され、イベントの配布が完了する.サブエレメントのdispatchTouchEventメソッドがdispatchTransformedTouchEventで呼び出されます
final int oldAction = event.getAction();
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);
return handled;
}
dispatchTransformedTouchEventがtrueを返す場合、すなわちサブViewはこのACTION_を処理するDOWNイベントでは、次のifブランチのコードを実行します.
if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) {
// Child wants to receive touch within its bounds.
mLastTouchDownTime = ev.getDownTime();
if (preorderedList != null) {
// childIndex points into presorted list, find original index
for (int j = 0; j < childrenCount; j++) {
if (children[childIndex] == mChildren[j]) {
mLastTouchDownIndex = j;
break;
}
}
} else {
mLastTouchDownIndex = childIndex;
}
mLastTouchDownX = ev.getX();
mLastTouchDownY = ev.getY();
newTouchTarget = addTouchTarget(child, idBitsToAssign);
alreadyDispatchedToNewTouchTarget = true;
break;
}
子ViewがACTIONを処理したらDOWNイベントでは、addTouchTargetメソッドでmFirstTouchTargetがこのサブViewに割り当てられます.コードは次のとおりです.
private TouchTarget addTouchTarget(View child, int pointerIdBits) {
TouchTarget target = TouchTarget.obtain(child, pointerIdBits);
target.next = mFirstTouchTarget;
mFirstTouchTarget = target;
return target;
}