View.onTouchEvent
程序员文章站
2022-04-19 10:36:36
public boolean onTouchEvent(MotionEvent event) { //获取基本信息(没考虑多点,仅考虑单点)坐标&action final float x = event.getX(); final float y = event.getY(); final int viewFlags = mViewFlags; final int action = event.getAction(); //判断是否可点击,长....
public boolean onTouchEvent(MotionEvent event) {
//获取基本信息(没考虑多点,仅考虑单点) 坐标&action
final float x = event.getX();
final float y = event.getY();
final int viewFlags = mViewFlags;
final int action = event.getAction();
//判断是否可点击,长按和上下文点击(上下文菜单,鼠标右键)
final boolean clickable = ((viewFlags & CLICKABLE) == CLICKABLE
|| (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
|| (viewFlags & CONTEXT_CLICKABLE) == CONTEXT_CLICKABLE;
//如果是不可用的,直接把事件消费掉,不再向下传递
if ((viewFlags & ENABLED_MASK) == DISABLED) {
if (action == MotionEvent.ACTION_UP && (mPrivateFlags & PFLAG_PRESSED) != 0) {
setPressed(false);
}
mPrivateFlags3 &= ~PFLAG3_FINGER_DOWN;
// A disabled view that is clickable still consumes the touch
// events, it just doesn't respond to them.
return clickable;
}
//是否有TouchDelegate,有就消费 -TouchDelegate:点击代理
if (mTouchDelegate != null) {
if (mTouchDelegate.onTouchEvent(event)) {
return true;
}
}
//TOOLTIP:解释文本
if (clickable || (viewFlags & TOOLTIP) == TOOLTIP) {
switch (action) {
case MotionEvent.ACTION_UP:
//手触摸到屏幕标记置空
mPrivateFlags3 &= ~PFLAG3_FINGER_DOWN;
//抬手后 tooltip延迟一会消失
if ((viewFlags & 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;
//如果可以获取焦点 可在实体按键中获取焦点 && 当前没有焦点
if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
focusTaken = requestFocus(); //请求焦点
}
//如果是预按下 up时设置按下
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();
}
//如果是预按下 抬起 效果等4帧
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:
//判断是否触摸到屏幕:存在实体按键->标记为手指按下
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(); //CheckForTap run:还原预按下状态,设置为按下状态,设置一个长按等待器
}
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); //0减去已等待
}
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) {
//热点改变:如水波纹中心
drawableHotspotChanged(x, y);
}
// Be lenient about moving outside of buttons
//手指移出了控件区域
if (!pointInView(x, y, mTouchSlop)) { //mTouchSlop:溢出宽容距离
// Outside button
// Remove any future long press/tap checks
//监听移除和flag清除
removeTapCallback();
removeLongPressCallback();
if ((mPrivateFlags & PFLAG_PRESSED) != 0) {
setPressed(false);
}
mPrivateFlags3 &= ~PFLAG3_FINGER_DOWN;
}
break;
}
return true;
}
return false;
}
本文地址:https://blog.csdn.net/rui_yi/article/details/107212489
下一篇: 简单优雅的搭建个人博客