首页 > 代码库 > Android L(5.0)源码之手势识别onTouchEvent
Android L(5.0)源码之手势识别onTouchEvent
onTouchEvent同样也是在view中定义的一个方法。处理传递到view 的手势事件。手势事件类型包括ACTION_DOWN,ACTION_MOVE,ACTION_UP,ACTION_CANCEL等事件。
贴上android 5.0源代码
1 public boolean onTouchEvent(MotionEvent event) { 2 final float x = event.getX(); 3 final float y = event.getY(); 4 final int viewFlags = mViewFlags; 5 6 if ((viewFlags & ENABLED_MASK) == DISABLED) { 7 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PFLAG_PRESSED) != 0) { 8 setPressed(false); 9 } 10 // A disabled view that is clickable still consumes the touch 11 // events, it just doesn‘t respond to them. 12 return (((viewFlags & CLICKABLE) == CLICKABLE || 13 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)); 14 } 15 16 if (mTouchDelegate != null) { 17 if (mTouchDelegate.onTouchEvent(event)) { 18 return true; 19 } 20 } 21 22 if (((viewFlags & CLICKABLE) == CLICKABLE || 23 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) { 24 switch (event.getAction()) { 25 case MotionEvent.ACTION_UP: 26 boolean prepressed = (mPrivateFlags & PFLAG_PREPRESSED) != 0; 27 if ((mPrivateFlags & PFLAG_PRESSED) != 0 || prepressed) { 28 // take focus if we don‘t have it already and we should in 29 // touch mode. 30 boolean focusTaken = false; 31 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) { 32 focusTaken = requestFocus(); 33 } 34 35 if (prepressed) { 36 // The button is being released before we actually 37 // showed it as pressed. Make it show the pressed 38 // state now (before scheduling the click) to ensure 39 // the user sees it. 40 setPressed(true, x, y); 41 } 42 43 if (!mHasPerformedLongPress) { 44 // This is a tap, so remove the longpress check 45 removeLongPressCallback(); 46 47 // Only perform take click actions if we were in the pressed state 48 if (!focusTaken) { 49 // Use a Runnable and post this rather than calling 50 // performClick directly. This lets other visual state 51 // of the view update before click actions start. 52 if (mPerformClick == null) { 53 mPerformClick = new PerformClick(); 54 } 55 if (!post(mPerformClick)) { 56 performClick(); 57 } 58 } 59 } 60 61 if (mUnsetPressedState == null) { 62 mUnsetPressedState = new UnsetPressedState(); 63 } 64 65 if (prepressed) { 66 postDelayed(mUnsetPressedState, 67 ViewConfiguration.getPressedStateDuration()); 68 } else if (!post(mUnsetPressedState)) { 69 // If the post failed, unpress right now 70 mUnsetPressedState.run(); 71 } 72 73 removeTapCallback(); 74 } 75 break; 76 77 case MotionEvent.ACTION_DOWN: 78 mHasPerformedLongPress = false; 79 80 if (performButtonActionOnTouchDown(event)) { 81 break; 82 } 83 84 // Walk up the hierarchy to determine if we‘re inside a scrolling container. 85 boolean isInScrollingContainer = isInScrollingContainer(); 86 87 // For views inside a scrolling container, delay the pressed feedback for 88 // a short period in case this is a scroll. 89 if (isInScrollingContainer) { 90 mPrivateFlags |= PFLAG_PREPRESSED; 91 if (mPendingCheckForTap == null) { 92 mPendingCheckForTap = new CheckForTap(); 93 } 94 mPendingCheckForTap.x = event.getX(); 95 mPendingCheckForTap.y = event.getY(); 96 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout()); 97 } else { 98 // Not inside a scrolling container, so show the feedback right away 99 setPressed(true, x, y);100 checkForLongClick(0);101 }102 break;103 104 case MotionEvent.ACTION_CANCEL:105 setPressed(false);106 removeTapCallback();107 removeLongPressCallback();108 break;109 110 case MotionEvent.ACTION_MOVE:111 drawableHotspotChanged(x, y);112 113 // Be lenient about moving outside of buttons114 if (!pointInView(x, y, mTouchSlop)) {115 // Outside button116 removeTapCallback();117 if ((mPrivateFlags & PFLAG_PRESSED) != 0) {118 // Remove any future long press/tap checks119 removeLongPressCallback();120 121 setPressed(false);122 }123 }124 break;125 }126 127 return true;128 }129 130 return false;131 }
Android L(5.0)源码之手势识别onTouchEvent
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。