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 buttons
114                     if (!pointInView(x, y, mTouchSlop)) {
115                         // Outside button
116                         removeTapCallback();
117                         if ((mPrivateFlags & PFLAG_PRESSED) != 0) {
118                             // Remove any future long press/tap checks
119                             removeLongPressCallback();
120 
121                             setPressed(false);
122                         }
123                     }
124                     break;
125             }
126 
127             return true;
128         }
129 
130         return false;
131     }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。