本文參考【張鴻洋的博客】的
1. View的事件分發(fā)
View的onTouchEvent:
public boolean onTouchEvent(MotionEvent event) {
final int viewFlags = mViewFlags;
if ((viewFlags & ENABLED_MASK) == DISABLED) {
// A disabled view that is clickable still consumes the touch
// events, it just doesn't respond to them.
return (((viewFlags & CLICKABLE) == CLICKABLE ||
(viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
}// 這一塊值纱,如果當(dāng)前view是disable狀態(tài)且是可點(diǎn)擊的則會(huì)消費(fèi)掉事件
if (mTouchDelegate != null) {
if (mTouchDelegate.onTouchEvent(event)) {
return true;
}
}// 如果設(shè)置了mTouchDelegate,則會(huì)將事件交給代理者處理奕塑,直接return true寝贡,如果大家希望自己的View增加它的touch范圍圃泡,可以嘗試使用TouchDelegate
if (((viewFlags & CLICKABLE) == CLICKABLE ||
(viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {// 如果我們的View可以點(diǎn)擊或者可以長按,則最終一定return true(即消費(fèi)掉事件)
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
if ((mPrivateFlags & PRESSED) != 0 || prepressed) {//判斷mPrivateFlags是否包含PREPRESSED缤弦,如果包含PRESSED或者PREPRESSED則進(jìn)入執(zhí)行體,也就是無論是115ms內(nèi)或者之后抬起都會(huì)進(jìn)入執(zhí)行體鞍匾。
// take focus if we don't have it already and we should in
// touch mode.
boolean focusTaken = false;
if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
focusTaken = requestFocus();
}
if (!mHasPerformedLongPress) {// 如果長按沒被執(zhí)行則不再處理長按事件
// This is a tap, so remove the longpress check 移除長按的檢測
removeLongPressCallback();
// Only perform take click actions if we were in the pressed state
if (!focusTaken) {
// Use a Runnable and post this rather than calling
// performClick directly. This lets other visual state
// of the view update before click actions start.
if (mPerformClick == null) {
mPerformClick = new PerformClick();
}
if (!post(mPerformClick)) {
performClick();
}
}
}
if (mUnsetPressedState == null) {
mUnsetPressedState = new UnsetPressedState();// 這個(gè)是把我們的mPrivateFlags中的PRESSED取消呛凶,然后刷新背景,把setPress轉(zhuǎn)發(fā)下去行贪。
}
if (prepressed) {// 如果按壓的時(shí)間不夠ViewConfiguration.getTapTimeout()
mPrivateFlags |= PRESSED;
refreshDrawableState();
postDelayed(mUnsetPressedState,
ViewConfiguration.getPressedStateDuration());// 延時(shí)執(zhí)行mUnsetPressedState.run()
} else if (!post(mUnsetPressedState)) {
// If the post failed, unpress right now
mUnsetPressedState.run();// 立刻執(zhí)行mUnsetPressedState.run()
}// 也就是不管咋樣漾稀,最后mUnsetPressedState.run()都會(huì)執(zhí)行;
removeTapCallback();
}
break;
case MotionEvent.ACTION_DOWN:
if (mPendingCheckForTap == null) {
mPendingCheckForTap = new CheckForTap();
}
mPrivateFlags |= PREPRESSED;// 給mPrivateFlags設(shè)置一個(gè)PREPRESSED的標(biāo)識(shí)(這個(gè)mPrivateFlags會(huì)在ViewConfiguration.getTapTimeout()的延時(shí)之后被換為PRESSED)
mHasPerformedLongPress = false;// 設(shè)置mHasPerformedLongPress=false建瘫;表示長按事件還未觸發(fā)崭捍;
postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());// 發(fā)送一個(gè)延遲為ViewConfiguration.getTapTimeout()的延遲消息,到達(dá)延時(shí)時(shí)間后會(huì)執(zhí)行CheckForTap里面的run方法
break;
case MotionEvent.ACTION_CANCEL:
mPrivateFlags &= ~PRESSED;
refreshDrawableState();
removeTapCallback();
break;
case MotionEvent.ACTION_MOVE:
final int x = (int) event.getX();
final int y = (int) event.getY();
// Be lenient about moving outside of buttons
int slop = mTouchSlop;
if ((x < 0 - slop) || (x >= getWidth() + slop) ||
(y < 0 - slop) || (y >= getHeight() + slop)) {// 判斷當(dāng)然觸摸點(diǎn)有沒有移出我們的View
// Outside button 1啰脚、執(zhí)行removeTapCallback(); 2殷蛇、然后判斷是否包含PRESSED標(biāo)識(shí),如果包含橄浓,移除長按的檢查:removeLongPressCallback();3粒梦、最后把mPrivateFlags中PRESSED標(biāo)識(shí)去除,刷新背景荸实;
removeTapCallback();
if ((mPrivateFlags & PRESSED) != 0) {
// Remove any future long press/tap checks
removeLongPressCallback();
// Need to switch from pressed to not pressed
mPrivateFlags &= ~PRESSED;
refreshDrawableState();
}
}
break;
}
return true;
}
return false;
}
private final class CheckForTap implements Runnable {
public void run() {//取消mPrivateFlags的PREPRESSED匀们,然后設(shè)置PRESSED標(biāo)識(shí),刷新背景泪勒,如果View支持長按事件昼蛀,則再發(fā)一個(gè)延時(shí)消息,檢測長按圆存;
mPrivateFlags &= ~PREPRESSED;
mPrivateFlags |= PRESSED;
refreshDrawableState();
if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
postCheckForLongClick(ViewConfiguration.getTapTimeout());
}
}
}
class CheckForLongPress implements Runnable {
private int mOriginalWindowAttachCount;
public void run() {
// 1叼旋、如果此時(shí)設(shè)置了長按的回調(diào),則執(zhí)行長按時(shí)的回調(diào)沦辙,且如果長按的回調(diào)返回true;才把mHasPerformedLongPress置為ture夫植;
// 2、否則,如果沒有設(shè)置長按回調(diào)或者長按回調(diào)返回的是false详民;則mHasPerformedLongPress依然是false;
if (isPressed() && (mParent != null)
&& mOriginalWindowAttachCount == mWindowAttachCount) {
if (performLongClick()) {
mHasPerformedLongPress = true;
}
}
}
onTouchEvent中的DOWN,MOVE,UP
DOWN時(shí):
a延欠、首先設(shè)置標(biāo)志為PREPRESSED,設(shè)置mHasPerformedLongPress=false ;然后發(fā)出一個(gè)115ms后的mPendingCheckForTap沈跨;
b由捎、如果115ms內(nèi)沒有觸發(fā)UP,則將標(biāo)志置為PRESSED饿凛,清除PREPRESSED標(biāo)志狞玛,同時(shí)發(fā)出一個(gè)延時(shí)為500-115ms的,檢測長按任務(wù)消息涧窒;
c心肪、如果500ms內(nèi)(從DOWN觸發(fā)開始算),則會(huì)觸發(fā)LongClickListener:
此時(shí)如果LongClickListener不為null纠吴,則會(huì)執(zhí)行回調(diào)硬鞍,同時(shí)如果LongClickListener.onClick返回true,才把mHasPerformedLongPress設(shè)置為true;否則mHasPerformedLongPress依然為false;
MOVE時(shí):
主要就是檢測用戶是否劃出控件戴已,如果劃出了:
115ms內(nèi)固该,直接移除mPendingCheckForTap;
115ms后恭陡,則將標(biāo)志中的PRESSED去除蹬音,同時(shí)移除長按的檢查:removeLongPressCallback();
UP時(shí):
a、如果115ms內(nèi)休玩,觸發(fā)UP著淆,此時(shí)標(biāo)志為PREPRESSED,則執(zhí)行UnsetPressedState拴疤,setPressed(false);會(huì)把setPress轉(zhuǎn)發(fā)下去永部,可以在View中復(fù)寫dispatchSetPressed方法接收;
b呐矾、如果是115ms-500ms間苔埋,即長按還未發(fā)生,則首先移除長按檢測蜒犯,執(zhí)行onClick回調(diào)组橄;
c、如果是500ms以后罚随,那么有兩種情況:
i.設(shè)置了onLongClickListener玉工,且onLongClickListener.onClick返回true,則點(diǎn)擊事件OnClick事件無法觸發(fā)淘菩;
ii.沒有設(shè)置onLongClickListener或者onLongClickListener.onClick返回false遵班,則點(diǎn)擊事件OnClick事件依然可以觸發(fā);d、最后執(zhí)行mUnsetPressedState.run()狭郑,將setPressed傳遞下去腹暖,然后將PRESSED標(biāo)識(shí)去除;
整個(gè)View的事件轉(zhuǎn)發(fā)流程是:
View.dispatchTouchEvent->View.setOnTouchListener->View.onTouchEvent
在dispatchTouchEvent中會(huì)進(jìn)行OnTouchListener的判斷翰萨,如果OnTouchListener不為null且返回true脏答,則表示事件被消費(fèi),onTouchEvent不會(huì)被執(zhí)行亩鬼;否則執(zhí)行onTouchEvent以蕴。
2. ViewGroup的事件分發(fā)
-
基本原理,如下圖
ps:如果事件分發(fā)中有控件消費(fèi)掉了事件(也就是onTouchEvent返回了true)辛孵,那么后續(xù)的事件中,到達(dá)該控件時(shí)事件就不會(huì)繼續(xù)分發(fā)下去赡磅,而是會(huì)直接調(diào)用onTouchEvent方法魄缚,如果該控件是ViewGroup的話,onInterceptTouchEvent也不會(huì)執(zhí)行