国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

View源碼-Touch事件

Barry_Ng / 1193人閱讀

摘要:接下來查看方法的源碼里面的處理如下如果則返回,即該對觸摸事件不予處理。顯示了提示框的話則隱藏如果不可點擊,則移除長按事件的檢測,返回否則繼續向下走。當前是點擊事件,所以移除長按事件的檢測。關于源碼中的事件,可以參考文章源碼事件

在Android-27中查看源碼:

首先我們來查看單個View的觸摸事件的處理,在View的dispatchTouchEvent方法中看看源碼是如何處理的。

</>復制代碼

  1. public boolean dispatchTouchEvent(MotionEvent event) {
  2. // If the event should be handled by accessibility focus first.
  3. if (event.isTargetAccessibilityFocus()) {
  4. // We don"t have focus or no virtual descendant has it, do not handle the event.
  5. if (!isAccessibilityFocusedViewOrHost()) {
  6. return false;
  7. }
  8. // We have focus and got the event, then use normal event dispatch.
  9. event.setTargetAccessibilityFocus(false);
  10. }
  11. boolean result = false;
  12. if (mInputEventConsistencyVerifier != null) {
  13. mInputEventConsistencyVerifier.onTouchEvent(event, 0);
  14. }
  15. final int actionMasked = event.getActionMasked();
  16. if (actionMasked == MotionEvent.ACTION_DOWN) {
  17. // Defensive cleanup for new gesture
  18. stopNestedScroll();
  19. }
  20. if (onFilterTouchEventForSecurity(event)) {
  21. if ((mViewFlags & ENABLED_MASK) == ENABLED && handleScrollBarDragging(event)) {
  22. result = true;
  23. }
  24. //noinspection SimplifiableIfStatement
  25. ListenerInfo li = mListenerInfo;
  26. if (li != null && li.mOnTouchListener != null
  27. && (mViewFlags & ENABLED_MASK) == ENABLED
  28. && li.mOnTouchListener.onTouch(this, event)) {
  29. result = true;
  30. }
  31. if (!result && onTouchEvent(event)) {
  32. result = true;
  33. }
  34. }
  35. if (!result && mInputEventConsistencyVerifier != null) {
  36. mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
  37. }
  38. // Clean up after nested scrolls if this is the end of a gesture;
  39. // also cancel it if we tried an ACTION_DOWN but we didn"t want the rest
  40. // of the gesture.
  41. if (actionMasked == MotionEvent.ACTION_UP ||
  42. actionMasked == MotionEvent.ACTION_CANCEL ||
  43. (actionMasked == MotionEvent.ACTION_DOWN && !result)) {
  44. stopNestedScroll();
  45. }
  46. return result;
  47. }

dispatchTouchEvent處理過程如下:

如果設置了OnTouchListener和enabled為true,并且onTouch返回為true,即該View在OnTouchListener的onTouch方法中處理了觸摸事件。

如果onTouchEvent返回為true,即該View在onTouchEvent中處理了觸摸事件。

如果觸摸事件在該View中得到處理則返回true,否則返回false(即該View對觸摸事件不予處理)。

接下來查看onTouchEvent方法的源碼:

</>復制代碼

  1. public boolean onTouchEvent(MotionEvent event) {
  2. final float x = event.getX();
  3. final float y = event.getY();
  4. final int viewFlags = mViewFlags;
  5. final int action = event.getAction();
  6. final boolean clickable = ((viewFlags & CLICKABLE) == CLICKABLE
  7. || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
  8. || (viewFlags & CONTEXT_CLICKABLE) == CONTEXT_CLICKABLE;
  9. if ((viewFlags & ENABLED_MASK) == DISABLED) {
  10. if (action == MotionEvent.ACTION_UP && (mPrivateFlags & PFLAG_PRESSED) != 0) {
  11. setPressed(false);
  12. }
  13. mPrivateFlags3 &= ~PFLAG3_FINGER_DOWN;
  14. // A disabled view that is clickable still consumes the touch
  15. // events, it just doesn"t respond to them.
  16. return clickable;
  17. }
  18. if (mTouchDelegate != null) {
  19. if (mTouchDelegate.onTouchEvent(event)) {
  20. return true;
  21. }
  22. }
  23. if (clickable || (viewFlags & TOOLTIP) == TOOLTIP) {
  24. switch (action) {
  25. case MotionEvent.ACTION_UP:
  26. mPrivateFlags3 &= ~PFLAG3_FINGER_DOWN;
  27. if ((viewFlags & TOOLTIP) == TOOLTIP) {
  28. handleTooltipUp();
  29. }
  30. if (!clickable) {
  31. removeTapCallback();
  32. removeLongPressCallback();
  33. mInContextButtonPress = false;
  34. mHasPerformedLongPress = false;
  35. mIgnoreNextUpEvent = false;
  36. break;
  37. }
  38. boolean prepressed = (mPrivateFlags & PFLAG_PREPRESSED) != 0;
  39. if ((mPrivateFlags & PFLAG_PRESSED) != 0 || prepressed) {
  40. // take focus if we don"t have it already and we should in
  41. // touch mode.
  42. boolean focusTaken = false;
  43. if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
  44. focusTaken = requestFocus();
  45. }
  46. if (prepressed) {
  47. // The button is being released before we actually
  48. // showed it as pressed. Make it show the pressed
  49. // state now (before scheduling the click) to ensure
  50. // the user sees it.
  51. setPressed(true, x, y);
  52. }
  53. if (!mHasPerformedLongPress && !mIgnoreNextUpEvent) {
  54. // This is a tap, so remove the longpress check
  55. removeLongPressCallback();
  56. // Only perform take click actions if we were in the pressed state
  57. if (!focusTaken) {
  58. // Use a Runnable and post this rather than calling
  59. // performClick directly. This lets other visual state
  60. // of the view update before click actions start.
  61. if (mPerformClick == null) {
  62. mPerformClick = new PerformClick();
  63. }
  64. if (!post(mPerformClick)) {
  65. performClick();
  66. }
  67. }
  68. }
  69. if (mUnsetPressedState == null) {
  70. mUnsetPressedState = new UnsetPressedState();
  71. }
  72. if (prepressed) {
  73. postDelayed(mUnsetPressedState,
  74. ViewConfiguration.getPressedStateDuration());
  75. } else if (!post(mUnsetPressedState)) {
  76. // If the post failed, unpress right now
  77. mUnsetPressedState.run();
  78. }
  79. removeTapCallback();
  80. }
  81. mIgnoreNextUpEvent = false;
  82. break;
  83. case MotionEvent.ACTION_DOWN:
  84. if (event.getSource() == InputDevice.SOURCE_TOUCHSCREEN) {
  85. mPrivateFlags3 |= PFLAG3_FINGER_DOWN;
  86. }
  87. mHasPerformedLongPress = false;
  88. if (!clickable) {
  89. checkForLongClick(0, x, y);
  90. break;
  91. }
  92. if (performButtonActionOnTouchDown(event)) {
  93. break;
  94. }
  95. // Walk up the hierarchy to determine if we"re inside a scrolling container.
  96. boolean isInScrollingContainer = isInScrollingContainer();
  97. // For views inside a scrolling container, delay the pressed feedback for
  98. // a short period in case this is a scroll.
  99. if (isInScrollingContainer) {
  100. mPrivateFlags |= PFLAG_PREPRESSED;
  101. if (mPendingCheckForTap == null) {
  102. mPendingCheckForTap = new CheckForTap();
  103. }
  104. mPendingCheckForTap.x = event.getX();
  105. mPendingCheckForTap.y = event.getY();
  106. postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
  107. } else {
  108. // Not inside a scrolling container, so show the feedback right away
  109. setPressed(true, x, y);
  110. checkForLongClick(0, x, y);
  111. }
  112. break;
  113. case MotionEvent.ACTION_CANCEL:
  114. if (clickable) {
  115. setPressed(false);
  116. }
  117. removeTapCallback();
  118. removeLongPressCallback();
  119. mInContextButtonPress = false;
  120. mHasPerformedLongPress = false;
  121. mIgnoreNextUpEvent = false;
  122. mPrivateFlags3 &= ~PFLAG3_FINGER_DOWN;
  123. break;
  124. case MotionEvent.ACTION_MOVE:
  125. if (clickable) {
  126. drawableHotspotChanged(x, y);
  127. }
  128. // Be lenient about moving outside of buttons
  129. if (!pointInView(x, y, mTouchSlop)) {
  130. // Outside button
  131. // Remove any future long press/tap checks
  132. removeTapCallback();
  133. removeLongPressCallback();
  134. if ((mPrivateFlags & PFLAG_PRESSED) != 0) {
  135. setPressed(false);
  136. }
  137. mPrivateFlags3 &= ~PFLAG3_FINGER_DOWN;
  138. }
  139. break;
  140. }
  141. return true;
  142. }
  143. return false;
  144. }

onTouchEvent里面的處理如下:

如果setEnabled(false),則返回false,即該View對觸摸事件不予處理。

如果設置了TouchDelegate,并且TouchDelegate的onTouchEvent返回true,則返回true,即由TouchDelegate中的onTouchEvent方法處理了觸摸事件。否則繼續向下。

如果該View不可點擊并且(viewFlags & TOOLTIP) != TOOLTIP(即當停留或長按在該view時不可顯示提示框),則返回false,即該View對觸摸事件不予處理。否則繼續向下。

根據觸摸事件的分類進行處理:

MotionEvent.ACTION_DOWN:

不可點擊或者不在可滑動的容器內:如果LONG_CLICKABLE為true則執行OnLongClickListener的onLongClick方法;或者設置了viewFlags為TOOLTIP,則執行showTooltip(顯示提示框)。然后返回true,即該View處理了觸摸事件,否則繼續向下走。

MotionEvent.ACTION_MOVE:在手指移動的過程中判斷是否仍在當前view的范圍。

MotionEvent.ACTION_UP:

顯示了提示框的話則隱藏;

如果不可點擊,則移除長按事件的檢測,返回true,否則繼續向下走。

當前是點擊事件,所以移除長按事件的檢測。然后執行performClick方法,返回true,即該View處理了觸摸事件。

關于ViewGroup源碼中的Touch事件,可以參考文章:ViewGroup源碼-Touch事件

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://m.specialneedsforspecialkids.com/yun/72174.html

相關文章

  • ViewGroup源碼-Touch事件

    摘要:在中查看源碼在看完源碼的觸摸事件源碼事件后,我們接著來看看容器類的觸摸事件。同樣的先來看源碼中的方法首先,會判斷是否不允許攔截,可以通過進行設置,默認允許攔截。 在Android-27中查看源碼: 在看完View源碼的觸摸事件(View源碼-Touch事件)后,我們接著來看看容器類View的觸摸事件。因為容器類的View都繼承自ViewGroup,所以我們在ViewGroup的源碼中來...

    rozbo 評論0 收藏0
  • View事件機制分析

    摘要:注意,事件分發是向下傳遞的,也就是父到子的順序。事件分發機制的本質是要解決,點擊事件由哪個對象發出,經過哪些對象,最終達到哪個對象并最終得到處理。表示以及分發給其中在內部完成被賦值。會自己處理事件。 目錄介紹 01.Android中事件分發順序 1.1 事件分發的對象是誰 1.2 事件分發的本質 1.3 事件在哪些對象間進行傳遞 1.4 事件分發過程涉及方法 1.5 Androi...

    bergwhite 評論0 收藏0
  • View事件機制源碼分析

    摘要:當不攔截事件的時候,事件會向下分發交由它的子或進行處理。表示以及分發給其中在內部完成被賦值。會自己處理事件。 目錄介紹 01.Android中事件分發順序 02.Activity的事件分發機制 2.1 源碼分析 2.2 點擊事件調用順序 2.3 得出結論 03.ViewGroup事件的分發機制 3.1 看一下這個案例 3.2 源碼分析 3.3 得出結論 04.Vie...

    antz 評論0 收藏0

發表評論

0條評論

最新活動
閱讀需要支付1元查看
<