• 2023-08-23 AndroidR 自主研究出来的三手指下滑截屏功能


    一、检测三个手指下滑消息,SystemGesturesPointerEventListener里面处理手势下滑消息frameworks\base\services\core\java\com\android\server\wm\SystemGesturesPointerEventListener.java

     二、接收三手指滑动信息,然后发送截屏广播frameworks\base\services\core\java\com\android\server\wm\DisplayPolicy.java

    SystemGesturesPointerEventListener

     

     三、检测到三个手指的时候,拦截手指下滑消息,不然截屏的同时会把界面下滑,如果是在菜单界面,会把内容下滑。我们需要截屏的时候保持原先的界面。

    四、测试log,三指滑下,截屏。 

    五、测试效果,三指滑下成功截屏。

    六、整个过程重需要修改的内容

    1. diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java
    2. old mode 100644
    3. new mode 100755
    4. index ca424e7..eb41453
    5. --- a/core/java/android/view/View.java
    6. +++ b/core/java/android/view/View.java
    7. @@ -14564,6 +14564,7 @@ public class View implements Drawable.Callback, KeyEvent.Callback,
    8. */
    9. @UnsupportedAppUsage
    10. public final boolean dispatchPointerEvent(MotionEvent event) {
    11. + Log.e(VIEW_LOG_TAG, "dispatchPointerEvent $$$$$$$$$$$$$$$$$$$$$");
    12. if (event.isTouchEvent()) {
    13. return dispatchTouchEvent(event);
    14. } else {
    15. diff --git a/core/java/android/view/ViewGroup.java b/core/java/android/view/ViewGroup.java
    16. old mode 100644
    17. new mode 100755
    18. index 77fedd7..2eb3eb2
    19. --- a/core/java/android/view/ViewGroup.java
    20. +++ b/core/java/android/view/ViewGroup.java
    21. @@ -201,6 +201,7 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager
    22. // Lazily created int[2] for dispatch to children
    23. private int[] mTempLocation;
    24. +
    25. // Layout animation
    26. private LayoutAnimationController mLayoutAnimationController;
    27. private Animation.AnimationListener mAnimationListener;
    28. @@ -239,6 +240,7 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager
    29. // True if the view group is capable of showing a tooltip and the pointer is directly
    30. // over the view group but not one of its child views.
    31. private boolean mTooltipHoveredSelf;
    32. + private boolean checkout_three_finger = false;
    33. /**
    34. * Internal flags.
    35. @@ -3286,6 +3288,18 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager
    36. && isOnScrollbarThumb(ev.getX(), ev.getY())) {
    37. return true;
    38. }
    39. + if(ev.getAction() == MotionEvent.ACTION_DOWN)
    40. + checkout_three_finger = false;
    41. +
    42. + //if((ev.getPointerCount() >= 3 )&&(ev.getAction() == MotionEvent.ACTION_MOVE)){
    43. + if(ev.getPointerCount() >= 3 ){
    44. + checkout_three_finger = true;
    45. + return true;
    46. + }
    47. + else if(checkout_three_finger && ev.getAction() == MotionEvent.ACTION_UP){
    48. + return true;
    49. + }
    50. +
    51. return false;
    52. }
    53. diff --git a/core/java/android/view/ViewRootImpl.java b/core/java/android/view/ViewRootImpl.java
    54. old mode 100644
    55. new mode 100755
    56. index 18e9ef0..573b67d
    57. --- a/core/java/android/view/ViewRootImpl.java
    58. +++ b/core/java/android/view/ViewRootImpl.java
    59. @@ -612,7 +612,7 @@ public final class ViewRootImpl implements ViewParent,
    60. private long mFpsStartTime = -1;
    61. private long mFpsPrevTime = -1;
    62. private int mFpsNumFrames;
    63. -
    64. + public boolean checkout_three_finger_up = false;
    65. private int mPointerIconType = PointerIcon.TYPE_NOT_SPECIFIED;
    66. private PointerIcon mCustomPointerIcon = null;
    67. @@ -6019,7 +6019,32 @@ public final class ViewRootImpl implements ViewParent,
    68. mAttachInfo.mUnbufferedDispatchRequested = false;
    69. mAttachInfo.mHandlingPointerEvent = true;
    70. - boolean handled = mView.dispatchPointerEvent(event);
    71. + Slog.d("View", "processPointerEvent dispatchPointerEvent ***************");
    72. +
    73. + boolean handled ;
    74. +/*
    75. + if(event.isTouchEvent())
    76. + {
    77. + if((event.getPointerCount() == 3 )&&(event.getAction() == MotionEvent.ACTION_MOVE))
    78. + {
    79. + checkout_three_finger_up = true;
    80. + handled = false ;
    81. + }
    82. + else {
    83. + if(checkout_three_finger_up && event.getAction() == MotionEvent.ACTION_UP)
    84. + handled = false ;
    85. + else
    86. + handled = mView.dispatchPointerEvent(event);
    87. + }
    88. +
    89. + if(event.getAction() == MotionEvent.ACTION_DOWN)
    90. + checkout_three_finger_up = false;
    91. +
    92. + }else
    93. +*/
    94. + handled = mView.dispatchPointerEvent(event);
    95. +
    96. + //boolean handled = mView.dispatchPointerEvent(event);
    97. maybeUpdatePointerIcon(event);
    98. maybeUpdateTooltip(event);
    99. mAttachInfo.mHandlingPointerEvent = false;
    100. diff --git a/services/core/java/com/android/server/wm/DisplayPolicy.java b/services/core/java/com/android/server/wm/DisplayPolicy.java
    101. old mode 100644
    102. new mode 100755
    103. index 29881cc..02cbb94
    104. --- a/services/core/java/com/android/server/wm/DisplayPolicy.java
    105. +++ b/services/core/java/com/android/server/wm/DisplayPolicy.java
    106. @@ -260,6 +260,7 @@ public class DisplayPolicy {
    107. private final DisplayContent mDisplayContent;
    108. private final Object mLock;
    109. private final Handler mHandler;
    110. + public final Handler mScreenshotHandler;
    111. private Resources mCurrentUserResources;
    112. @@ -520,10 +521,26 @@ public class DisplayPolicy {
    113. final Looper looper = UiThread.getHandler().getLooper();
    114. mHandler = new PolicyHandler(looper);
    115. + mScreenshotHandler = new Handler() {
    116. + public void handleMessage(Message msg) {
    117. + Slog.i(TAG, "mScreenshotHandler handleMessage msg.what:" + msg.what);
    118. + switch(msg.what){
    119. + case 1:
    120. + Intent mscreenshot = new Intent("android.intent.action.SCREENSHOT");
    121. + mContext.sendBroadcast(mscreenshot);
    122. + break;
    123. +
    124. + default:
    125. + break;
    126. + }
    127. + }
    128. + };
    129. +
    130. mSystemGestures = new SystemGesturesPointerEventListener(mContext, mHandler,
    131. new SystemGesturesPointerEventListener.Callbacks() {
    132. @Override
    133. public void onSwipeFromTop() {
    134. + Slog.i(TAG, "onSwipeFromTop: 0000000000000");
    135. synchronized (mLock) {
    136. if (mStatusBar != null) {
    137. requestTransientBars(mStatusBar);
    138. @@ -534,6 +551,7 @@ public class DisplayPolicy {
    139. @Override
    140. public void onSwipeFromBottom() {
    141. + Slog.i(TAG, "onSwipeFromBottom: 0000000000000");
    142. synchronized (mLock) {
    143. if (mNavigationBar != null
    144. && mNavigationBarPosition == NAV_BAR_BOTTOM) {
    145. @@ -579,6 +597,7 @@ public class DisplayPolicy {
    146. @Override
    147. public void onFling(int duration) {
    148. + Slog.i(TAG, "onFling: 0000000000000");
    149. if (mService.mPowerManagerInternal != null) {
    150. mService.mPowerManagerInternal.powerHint(
    151. PowerHint.INTERACTION, duration);
    152. @@ -597,15 +616,22 @@ public class DisplayPolicy {
    153. @Override
    154. public void onDown() {
    155. + Slog.i(TAG, "onDown: 0000000000000");
    156. final WindowOrientationListener listener = getOrientationListener();
    157. if (listener != null) {
    158. listener.onTouchStart();
    159. }
    160. }
    161. + @Override
    162. + public void onSwipeThreeFingerDown() {
    163. + Slog.i(TAG, "onSwipeThreeFingerDown: 0000000000000");
    164. + mScreenshotHandler.sendEmptyMessageDelayed(1,100);
    165. + }
    166. @Override
    167. public void onUpOrCancel() {
    168. final WindowOrientationListener listener = getOrientationListener();
    169. + Slog.i(TAG, "onUpOrCancel: 0000000000000");
    170. if (listener != null) {
    171. listener.onTouchEnd();
    172. }
    173. diff --git a/services/core/java/com/android/server/wm/SystemGesturesPointerEventListener.java b/services/core/java/com/android/server/wm/SystemGesturesPointerEventListener.java
    174. old mode 100644
    175. new mode 100755
    176. index f3859b4..4d71ac6
    177. --- a/services/core/java/com/android/server/wm/SystemGesturesPointerEventListener.java
    178. +++ b/services/core/java/com/android/server/wm/SystemGesturesPointerEventListener.java
    179. @@ -38,7 +38,7 @@ import android.widget.OverScroller;
    180. */
    181. class SystemGesturesPointerEventListener implements PointerEventListener {
    182. private static final String TAG = "SystemGestures";
    183. - private static final boolean DEBUG = false;
    184. + private static final boolean DEBUG = true;
    185. private static final long SWIPE_TIMEOUT_MS = 500;
    186. private static final int MAX_TRACKED_POINTERS = 32; // max per input system
    187. private static final int UNTRACKED_POINTER = -1;
    188. @@ -49,6 +49,7 @@ class SystemGesturesPointerEventListener implements PointerEventListener {
    189. private static final int SWIPE_FROM_BOTTOM = 2;
    190. private static final int SWIPE_FROM_RIGHT = 3;
    191. private static final int SWIPE_FROM_LEFT = 4;
    192. + private static final int SWIPE_DOWN = 5;
    193. private final Context mContext;
    194. private final Handler mHandler;
    195. @@ -142,6 +143,8 @@ class SystemGesturesPointerEventListener implements PointerEventListener {
    196. mDebugFireable = true;
    197. mDownPointers = 0;
    198. captureDown(event, 0);
    199. +
    200. + Slog.d(TAG, " MotionEvent.ACTION_DOWN:");
    201. if (mMouseHoveringAtEdge) {
    202. mMouseHoveringAtEdge = false;
    203. mCallbacks.onMouseLeaveFromEdge();
    204. @@ -150,6 +153,7 @@ class SystemGesturesPointerEventListener implements PointerEventListener {
    205. break;
    206. case MotionEvent.ACTION_POINTER_DOWN:
    207. captureDown(event, event.getActionIndex());
    208. + Slog.d(TAG, "MotionEvent.ACTION_POINTER_DOWN event.getPointerCount()="+event.getPointerCount());
    209. if (mDebugFireable) {
    210. mDebugFireable = event.getPointerCount() < 5;
    211. if (!mDebugFireable) {
    212. @@ -159,6 +163,7 @@ class SystemGesturesPointerEventListener implements PointerEventListener {
    213. }
    214. break;
    215. case MotionEvent.ACTION_MOVE:
    216. + Slog.d(TAG, " MotionEvent.ACTION_MOVE:");
    217. if (mSwipeFireable) {
    218. final int swipe = detectSwipe(event);
    219. mSwipeFireable = swipe == SWIPE_NONE;
    220. @@ -174,10 +179,16 @@ class SystemGesturesPointerEventListener implements PointerEventListener {
    221. } else if (swipe == SWIPE_FROM_LEFT) {
    222. if (DEBUG) Slog.d(TAG, "Firing onSwipeFromLeft");
    223. mCallbacks.onSwipeFromLeft();
    224. + } else if (swipe == SWIPE_DOWN) {
    225. + if (DEBUG) Slog.d(TAG, "Firing onSwipeDown event.getPointerCount()"+event.getPointerCount());
    226. + if(event.getPointerCount() >= 3 )
    227. + mCallbacks.onSwipeThreeFingerDown();
    228. }
    229. +
    230. }
    231. break;
    232. case MotionEvent.ACTION_HOVER_MOVE:
    233. + Slog.d(TAG, " MotionEvent.ACTION_HOVER_MOVE:");
    234. if (event.isFromSource(InputDevice.SOURCE_MOUSE)) {
    235. if (!mMouseHoveringAtEdge && event.getY() == 0) {
    236. mCallbacks.onMouseHoverAtTop();
    237. @@ -196,6 +207,7 @@ class SystemGesturesPointerEventListener implements PointerEventListener {
    238. case MotionEvent.ACTION_CANCEL:
    239. mSwipeFireable = false;
    240. mDebugFireable = false;
    241. + Slog.d(TAG, " MotionEvent.ACTION_UP ACTION_CANCEL event.getPointerCount()="+event.getPointerCount());
    242. mCallbacks.onUpOrCancel();
    243. break;
    244. default:
    245. @@ -269,6 +281,9 @@ class SystemGesturesPointerEventListener implements PointerEventListener {
    246. && y > fromY + mSwipeDistanceThreshold
    247. && elapsed < SWIPE_TIMEOUT_MS) {
    248. return SWIPE_FROM_TOP;
    249. + }else if (y > fromY + mSwipeDistanceThreshold
    250. + && elapsed < SWIPE_TIMEOUT_MS) {
    251. + return SWIPE_DOWN;
    252. }
    253. if (fromY >= screenHeight - mSwipeStartThreshold
    254. && y < fromY - mSwipeDistanceThreshold
    255. @@ -336,5 +351,6 @@ class SystemGesturesPointerEventListener implements PointerEventListener {
    256. void onMouseHoverAtBottom();
    257. void onMouseLeaveFromEdge();
    258. void onDebug();
    259. + void onSwipeThreeFingerDown();
    260. }
    261. }

    七、有价值的参考文章

    Android 触摸事件的分发_梁景杰Android的博客-CSDN博客_android触摸分发

    Android三指截屏实现,一个简单的三指截屏功能实现_阿機米德的博客-CSDN博客

    Android屏幕截图实现方式 系统截屏源码分析和三指截屏_Ansen360的博客-CSDN博客_android 截屏源码

  • 相关阅读:
    net+惠民线上诊疗系统 毕业设计-附源码161026
    Bert-vits2新版本V2.1英文模型本地训练以及中英文混合推理(mix)
    深度学习领域图像分割FCN(Fully Convolutional Networks for Semantic Segmentation)
    【JavaEE】Spring的创建和使用(保姆级手把手图解)
    MySQL - 为什么InnoDB选择B+树索引?Change buffer?
    APO的全面进化之路(2):走向IBP、建设端到端供应链计划体系
    如何避免大语言模型绕过知识库乱答的情况?LlamaIndex 原理与应用简介
    MySQL表的增删改查(进阶)
    系统架构设计师知识点总结:操作系统基本原理
    【RabbitMQ】——延迟队列
  • 原文地址:https://blog.csdn.net/qq_37858386/article/details/126490618