一、检测三个手指下滑消息,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,三指滑下,截屏。
五、测试效果,三指滑下成功截屏。
六、整个过程重需要修改的内容
- diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java
- old mode 100644
- new mode 100755
- index ca424e7..eb41453
- --- a/core/java/android/view/View.java
- +++ b/core/java/android/view/View.java
- @@ -14564,6 +14564,7 @@ public class View implements Drawable.Callback, KeyEvent.Callback,
- */
- @UnsupportedAppUsage
- public final boolean dispatchPointerEvent(MotionEvent event) {
- + Log.e(VIEW_LOG_TAG, "dispatchPointerEvent $$$$$$$$$$$$$$$$$$$$$");
- if (event.isTouchEvent()) {
- return dispatchTouchEvent(event);
- } else {
- diff --git a/core/java/android/view/ViewGroup.java b/core/java/android/view/ViewGroup.java
- old mode 100644
- new mode 100755
- index 77fedd7..2eb3eb2
- --- a/core/java/android/view/ViewGroup.java
- +++ b/core/java/android/view/ViewGroup.java
- @@ -201,6 +201,7 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager
- // Lazily created int[2] for dispatch to children
- private int[] mTempLocation;
-
- +
- // Layout animation
- private LayoutAnimationController mLayoutAnimationController;
- private Animation.AnimationListener mAnimationListener;
- @@ -239,6 +240,7 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager
- // True if the view group is capable of showing a tooltip and the pointer is directly
- // over the view group but not one of its child views.
- private boolean mTooltipHoveredSelf;
- + private boolean checkout_three_finger = false;
-
- /**
- * Internal flags.
- @@ -3286,6 +3288,18 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager
- && isOnScrollbarThumb(ev.getX(), ev.getY())) {
- return true;
- }
- + if(ev.getAction() == MotionEvent.ACTION_DOWN)
- + checkout_three_finger = false;
- +
- + //if((ev.getPointerCount() >= 3 )&&(ev.getAction() == MotionEvent.ACTION_MOVE)){
- + if(ev.getPointerCount() >= 3 ){
- + checkout_three_finger = true;
- + return true;
- + }
- + else if(checkout_three_finger && ev.getAction() == MotionEvent.ACTION_UP){
- + return true;
- + }
- +
- return false;
- }
-
- diff --git a/core/java/android/view/ViewRootImpl.java b/core/java/android/view/ViewRootImpl.java
- old mode 100644
- new mode 100755
- index 18e9ef0..573b67d
- --- a/core/java/android/view/ViewRootImpl.java
- +++ b/core/java/android/view/ViewRootImpl.java
- @@ -612,7 +612,7 @@ public final class ViewRootImpl implements ViewParent,
- private long mFpsStartTime = -1;
- private long mFpsPrevTime = -1;
- private int mFpsNumFrames;
- -
- + public boolean checkout_three_finger_up = false;
- private int mPointerIconType = PointerIcon.TYPE_NOT_SPECIFIED;
- private PointerIcon mCustomPointerIcon = null;
-
- @@ -6019,7 +6019,32 @@ public final class ViewRootImpl implements ViewParent,
-
- mAttachInfo.mUnbufferedDispatchRequested = false;
- mAttachInfo.mHandlingPointerEvent = true;
- - boolean handled = mView.dispatchPointerEvent(event);
- + Slog.d("View", "processPointerEvent dispatchPointerEvent ***************");
- +
- + boolean handled ;
- +/*
- + if(event.isTouchEvent())
- + {
- + if((event.getPointerCount() == 3 )&&(event.getAction() == MotionEvent.ACTION_MOVE))
- + {
- + checkout_three_finger_up = true;
- + handled = false ;
- + }
- + else {
- + if(checkout_three_finger_up && event.getAction() == MotionEvent.ACTION_UP)
- + handled = false ;
- + else
- + handled = mView.dispatchPointerEvent(event);
- + }
- +
- + if(event.getAction() == MotionEvent.ACTION_DOWN)
- + checkout_three_finger_up = false;
- +
- + }else
- +*/
- + handled = mView.dispatchPointerEvent(event);
- +
- + //boolean handled = mView.dispatchPointerEvent(event);
- maybeUpdatePointerIcon(event);
- maybeUpdateTooltip(event);
- mAttachInfo.mHandlingPointerEvent = false;
- diff --git a/services/core/java/com/android/server/wm/DisplayPolicy.java b/services/core/java/com/android/server/wm/DisplayPolicy.java
- old mode 100644
- new mode 100755
- index 29881cc..02cbb94
- --- a/services/core/java/com/android/server/wm/DisplayPolicy.java
- +++ b/services/core/java/com/android/server/wm/DisplayPolicy.java
- @@ -260,6 +260,7 @@ public class DisplayPolicy {
- private final DisplayContent mDisplayContent;
- private final Object mLock;
- private final Handler mHandler;
- + public final Handler mScreenshotHandler;
-
- private Resources mCurrentUserResources;
-
- @@ -520,10 +521,26 @@ public class DisplayPolicy {
-
- final Looper looper = UiThread.getHandler().getLooper();
- mHandler = new PolicyHandler(looper);
- + mScreenshotHandler = new Handler() {
- + public void handleMessage(Message msg) {
- + Slog.i(TAG, "mScreenshotHandler handleMessage msg.what:" + msg.what);
- + switch(msg.what){
- + case 1:
- + Intent mscreenshot = new Intent("android.intent.action.SCREENSHOT");
- + mContext.sendBroadcast(mscreenshot);
- + break;
- +
- + default:
- + break;
- + }
- + }
- + };
- +
- mSystemGestures = new SystemGesturesPointerEventListener(mContext, mHandler,
- new SystemGesturesPointerEventListener.Callbacks() {
- @Override
- public void onSwipeFromTop() {
- + Slog.i(TAG, "onSwipeFromTop: 0000000000000");
- synchronized (mLock) {
- if (mStatusBar != null) {
- requestTransientBars(mStatusBar);
- @@ -534,6 +551,7 @@ public class DisplayPolicy {
-
- @Override
- public void onSwipeFromBottom() {
- + Slog.i(TAG, "onSwipeFromBottom: 0000000000000");
- synchronized (mLock) {
- if (mNavigationBar != null
- && mNavigationBarPosition == NAV_BAR_BOTTOM) {
- @@ -579,6 +597,7 @@ public class DisplayPolicy {
-
- @Override
- public void onFling(int duration) {
- + Slog.i(TAG, "onFling: 0000000000000");
- if (mService.mPowerManagerInternal != null) {
- mService.mPowerManagerInternal.powerHint(
- PowerHint.INTERACTION, duration);
- @@ -597,15 +616,22 @@ public class DisplayPolicy {
-
- @Override
- public void onDown() {
- + Slog.i(TAG, "onDown: 0000000000000");
- final WindowOrientationListener listener = getOrientationListener();
- if (listener != null) {
- listener.onTouchStart();
- }
- }
- + @Override
- + public void onSwipeThreeFingerDown() {
- + Slog.i(TAG, "onSwipeThreeFingerDown: 0000000000000");
- + mScreenshotHandler.sendEmptyMessageDelayed(1,100);
- + }
-
- @Override
- public void onUpOrCancel() {
- final WindowOrientationListener listener = getOrientationListener();
- + Slog.i(TAG, "onUpOrCancel: 0000000000000");
- if (listener != null) {
- listener.onTouchEnd();
- }
- diff --git a/services/core/java/com/android/server/wm/SystemGesturesPointerEventListener.java b/services/core/java/com/android/server/wm/SystemGesturesPointerEventListener.java
- old mode 100644
- new mode 100755
- index f3859b4..4d71ac6
- --- a/services/core/java/com/android/server/wm/SystemGesturesPointerEventListener.java
- +++ b/services/core/java/com/android/server/wm/SystemGesturesPointerEventListener.java
- @@ -38,7 +38,7 @@ import android.widget.OverScroller;
- */
- class SystemGesturesPointerEventListener implements PointerEventListener {
- private static final String TAG = "SystemGestures";
- - private static final boolean DEBUG = false;
- + private static final boolean DEBUG = true;
- private static final long SWIPE_TIMEOUT_MS = 500;
- private static final int MAX_TRACKED_POINTERS = 32; // max per input system
- private static final int UNTRACKED_POINTER = -1;
- @@ -49,6 +49,7 @@ class SystemGesturesPointerEventListener implements PointerEventListener {
- private static final int SWIPE_FROM_BOTTOM = 2;
- private static final int SWIPE_FROM_RIGHT = 3;
- private static final int SWIPE_FROM_LEFT = 4;
- + private static final int SWIPE_DOWN = 5;
-
- private final Context mContext;
- private final Handler mHandler;
- @@ -142,6 +143,8 @@ class SystemGesturesPointerEventListener implements PointerEventListener {
- mDebugFireable = true;
- mDownPointers = 0;
- captureDown(event, 0);
- +
- + Slog.d(TAG, " MotionEvent.ACTION_DOWN:");
- if (mMouseHoveringAtEdge) {
- mMouseHoveringAtEdge = false;
- mCallbacks.onMouseLeaveFromEdge();
- @@ -150,6 +153,7 @@ class SystemGesturesPointerEventListener implements PointerEventListener {
- break;
- case MotionEvent.ACTION_POINTER_DOWN:
- captureDown(event, event.getActionIndex());
- + Slog.d(TAG, "MotionEvent.ACTION_POINTER_DOWN event.getPointerCount()="+event.getPointerCount());
- if (mDebugFireable) {
- mDebugFireable = event.getPointerCount() < 5;
- if (!mDebugFireable) {
- @@ -159,6 +163,7 @@ class SystemGesturesPointerEventListener implements PointerEventListener {
- }
- break;
- case MotionEvent.ACTION_MOVE:
- + Slog.d(TAG, " MotionEvent.ACTION_MOVE:");
- if (mSwipeFireable) {
- final int swipe = detectSwipe(event);
- mSwipeFireable = swipe == SWIPE_NONE;
- @@ -174,10 +179,16 @@ class SystemGesturesPointerEventListener implements PointerEventListener {
- } else if (swipe == SWIPE_FROM_LEFT) {
- if (DEBUG) Slog.d(TAG, "Firing onSwipeFromLeft");
- mCallbacks.onSwipeFromLeft();
- + } else if (swipe == SWIPE_DOWN) {
- + if (DEBUG) Slog.d(TAG, "Firing onSwipeDown event.getPointerCount()"+event.getPointerCount());
- + if(event.getPointerCount() >= 3 )
- + mCallbacks.onSwipeThreeFingerDown();
- }
- +
- }
- break;
- case MotionEvent.ACTION_HOVER_MOVE:
- + Slog.d(TAG, " MotionEvent.ACTION_HOVER_MOVE:");
- if (event.isFromSource(InputDevice.SOURCE_MOUSE)) {
- if (!mMouseHoveringAtEdge && event.getY() == 0) {
- mCallbacks.onMouseHoverAtTop();
- @@ -196,6 +207,7 @@ class SystemGesturesPointerEventListener implements PointerEventListener {
- case MotionEvent.ACTION_CANCEL:
- mSwipeFireable = false;
- mDebugFireable = false;
- + Slog.d(TAG, " MotionEvent.ACTION_UP ACTION_CANCEL event.getPointerCount()="+event.getPointerCount());
- mCallbacks.onUpOrCancel();
- break;
- default:
- @@ -269,6 +281,9 @@ class SystemGesturesPointerEventListener implements PointerEventListener {
- && y > fromY + mSwipeDistanceThreshold
- && elapsed < SWIPE_TIMEOUT_MS) {
- return SWIPE_FROM_TOP;
- + }else if (y > fromY + mSwipeDistanceThreshold
- + && elapsed < SWIPE_TIMEOUT_MS) {
- + return SWIPE_DOWN;
- }
- if (fromY >= screenHeight - mSwipeStartThreshold
- && y < fromY - mSwipeDistanceThreshold
- @@ -336,5 +351,6 @@ class SystemGesturesPointerEventListener implements PointerEventListener {
- void onMouseHoverAtBottom();
- void onMouseLeaveFromEdge();
- void onDebug();
- + void onSwipeThreeFingerDown();
- }
- }
七、有价值的参考文章
Android 触摸事件的分发_梁景杰Android的博客-CSDN博客_android触摸分发
Android三指截屏实现,一个简单的三指截屏功能实现_阿機米德的博客-CSDN博客
Android屏幕截图实现方式 系统截屏源码分析和三指截屏_Ansen360的博客-CSDN博客_android 截屏源码