源码解析
public boolean onTouchEvent(MotionEvent event) {
final float x = event.getX();
final float y = event.getY();
final int viewFlags = mViewFlags;
//View触摸不考虑多点触控
final int action = event.getAction();
//可点击 || 长按 || (长按菜单&&鼠标右键)
final boolean clickable = ((viewFlags & CLICKABLE) == CLICKABLE
|| (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
|| (viewFlags & CONTEXT_CLICKABLE) == CONTEXT_CLICKABLE;
//View不可用,返回clickable
if ((viewFlags & ENABLED_MASK) == DISABLED) {
if (action == MotionEvent.ACTION_UP && (mPrivateFlags & PFLAG_PRESSED) != 0) {
setPressed(false);
}
mPrivateFlags3 &= ~PFLAG3_FINGER_DOWN;
// 按钮本可点击但是被设置为禁用,仍然消耗掉事件, 只是不做出响应。
return clickable;
}
//点击代理
if (mTouchDelegate != null) {
if (mTouchDelegate.onTouchEvent(event)) {
return true;
}
}
//可点击 || tooltip(长按提示,api28引入)
if (clickable || (viewFlags & TOOLTIP) == TOOLTIP) {
switch (action) {
case MotionEvent.ACTION_UP:
mPrivateFlags3 &= ~PFLAG3_FINGER_DOWN;
if ((viewFlags & TOOLTIP) == TOOLTIP) {
//松手后隐藏tooltip
handleTooltipUp();
}
if (!clickable) {
removeTapCallback();
removeLongPressCallback();
mInContextButtonPress = false;
mHasPerformedLongPress = false;
mIgnoreNextUpEvent = false;
break;
}
boolean prepressed = (mPrivateFlags & PFLAG_PREPRESSED) != 0;
//按下状态 || 预按下状态
if ((mPrivateFlags & PFLAG_PRESSED) != 0 || prepressed) {
// take focus if we don't have it already and we should in
// touch mode.
boolean focusTaken = false;
//isFocusableInTouchMode(),实体按键,获取焦点
if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
focusTaken = requestFocus();
}
if (prepressed) {
// The button is being released before we actually
// showed it as pressed. Make it show the pressed
// state now (before scheduling the click) to ensure
// the user sees it.
//预按下状态设置为按下
setPressed(true, x, y);
}
//处理点击
if (!mHasPerformedLongPress && !mIgnoreNextUpEvent) {
// 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)) {
performClickInternal();
}
}
}
if (mUnsetPressedState == null) {
mUnsetPressedState = new UnsetPressedState();
}
if (prepressed) {
//预按下,延时取消按下状态,
postDelayed(mUnsetPressedState,
ViewConfiguration.getPressedStateDuration());
} else if (!post(mUnsetPressedState)) {
// If the post failed, unpress right now
mUnsetPressedState.run();
}
removeTapCallback();
}
mIgnoreNextUpEvent = false;
break;
case MotionEvent.ACTION_DOWN:
//DOWN事件是否是触摸产生,实体按键也可产生DOWN事件,
PFLAG3_FINGER_DOWN影响tooltip呼出位置
if (event.getSource() == InputDevice.SOURCE_TOUCHSCREEN) {
mPrivateFlags3 |= PFLAG3_FINGER_DOWN;
}
mHasPerformedLongPress = false;
//不可点击,设置长按监听(用于呼出tooltip),
if (!clickable) {
checkForLongClick(0, x, y);
break;
}
//检测鼠标右键点击
if (performButtonActionOnTouchDown(event)) {
break;
}
// Walk up the hierarchy to determine if we're inside a scrolling container.
boolean isInScrollingContainer = isInScrollingContainer();
// For views inside a scrolling container, delay the pressed feedback for
// a short period in case this is a scroll.
//在滑动控件中
if (isInScrollingContainer) {
//设置为预按下状态
mPrivateFlags |= PFLAG_PREPRESSED;
if (mPendingCheckForTap == null)
mPendingCheckForTap = new CheckForTap();
}
mPendingCheckForTap.x = event.getX();
mPendingCheckForTap.y = event.getY();
//延时设置按下状态
postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
}
//不在滑动控件中
else {
// Not inside a scrolling container, so show the feedback right away
//设置为按下状态
setPressed(true, x, y);
//设置长按检测监听
checkForLongClick(0, x, y);
}
break;
case MotionEvent.ACTION_CANCEL:
//重置
if (clickable) {
setPressed(false);
}
removeTapCallback();
removeLongPressCallback();
mInContextButtonPress = false;
mHasPerformedLongPress = false;
mIgnoreNextUpEvent = false;
mPrivateFlags3 &= ~PFLAG3_FINGER_DOWN;
break;
case MotionEvent.ACTION_MOVE:
if (clickable) {
//5.0移动水波纹中心位置
drawableHotspotChanged(x, y);
}
// Be lenient about moving outside of buttons
//是否在View范围内
if (!pointInView(x, y, mTouchSlop)) {
// Outside button
// Remove any future long press/tap checks
removeTapCallback();
removeLongPressCallback();
if ((mPrivateFlags & PFLAG_PRESSED) != 0) {
setPressed(false);
}
mPrivateFlags3 &= ~PFLAG3_FINGER_DOWN;
}
break;
}
return true;
}
return false;
}
1、返回值表示是否消耗当前事件组,标记事件的归属。
2、是否消费事件取决于 ACTION_DOWN 返回值 ,仅ACTION_DOWN返回值有效。
3、true,消耗ACTION_DOWN事件及后续事件,后续事件全部交由当前View处理。
4、false,放弃ACTION_DOWN事件及后续事件,后续事件不会再触发当前View的onTouchEvent方法。
5、ACTION_DOWN返回ture,ACTION_MOVE返回false,Activity onTouchEvent也会触发MOVE事件。
6、图1为手机横截面,黄色和蓝色同为灰色子控件,且黄色和蓝色有部分重叠,重叠区域黄色在上方。箭头为DOWN事件最远路径。
图1.png
网友评论