如何在Android 12 aosp系统源码中添加三指下滑截图功能
如何在Android 12 aosp系统源码中添加三指下滑截图功能
系统中截图api非常简单:
private static ScreenshotHelper sScreenshotHelper;
sScreenshotHelper = new ScreenshotHelper(mContext);
//调用
sScreenshotHelper.takeScreenshot(WindowManager.TAKE_SCREENSHOT_FULLSCREEN, true, true, mScreenShotHandler, null);
如果在系统中添加三指下滑监听了:
首先找到:
我们这里基于Android12 源码
首先找到:SystemGesturesPointerEventListener类,这里面有监听系统滑动手势的地方:
找到下面代码添加手势判断:
private int detectSwipe(int i, long time, float x, float y) {
final float fromX = mDownX[i];
final float fromY = mDownY[i];
final long elapsed = time - mDownTime[i];
if (DEBUG) Slog.d(TAG, "pointer " + mDownPointerId[i]
+ " moved (" + fromX + "->" + x + "," + fromY + "->" + y + ") in " + elapsed);
//添加三指下滑判断
float xDistance = Math.abs(x - fromX);
float yDistance = Math.abs(y - fromY);
if (fromY >= mSwipeStartThreshold * 2
&& yDistance > xDistance
&& y > fromY + mSwipeDistanceThreshold
&& elapsed < SWIPE_TIMEOUT_MS) {
return SWIPE_FROM_CENTER_TO_DOWN;
}
if (fromY <= mSwipeStartThreshold
&& y > fromY + mSwipeDistanceThreshold
&& elapsed < SWIPE_TIMEOUT_MS) {
return SWIPE_FROM_TOP;
}
if (fromY >= screenHeight - mSwipeStartThreshold
&& y < fromY - mSwipeDistanceThreshold
&& elapsed < SWIPE_TIMEOUT_MS) {
return SWIPE_FROM_BOTTOM;
}
if (fromX >= screenWidth - mSwipeStartThreshold
&& x < fromX - mSwipeDistanceThreshold
&& elapsed < SWIPE_TIMEOUT_MS) {
return SWIPE_FROM_RIGHT;
}
if (fromX <= mSwipeStartThreshold
&& x > fromX + mSwipeDistanceThreshold
&& elapsed < SWIPE_TIMEOUT_MS) {
return SWIPE_FROM_LEFT;
}
return SWIPE_NONE;
}
再找到这个方法:
public void onPointerEvent(MotionEvent event)
在里面添加代码:
case MotionEvent.ACTION_MOVE:
if (mSwipeFireable) {
final int swipe = detectSwipe(event);
mSwipeFireable = swipe == SWIPE_NONE;
if (swipe == SWIPE_FROM_TOP) {
if (DEBUG) Slog.d(TAG, "Firing onSwipeFromTop");
mCallbacks.onSwipeFromTop();
} else if (swipe == SWIPE_FROM_BOTTOM) {
if (DEBUG) Slog.d(TAG, "Firing onSwipeFromBottom");
mCallbacks.onSwipeFromBottom();
} else if (swipe == SWIPE_FROM_RIGHT) {
if (DEBUG) Slog.d(TAG, "Firing onSwipeFromRight");
mCallbacks.onSwipeFromRight();
} else if (swipe == SWIPE_FROM_LEFT) {
if (DEBUG) Slog.d(TAG, "Firing onSwipeFromLeft");
mCallbacks.onSwipeFromLeft();
} else if (swipe == SWIPE_FROM_CENTER_TO_DOWN) {
if (DEBUG) Slog.d(TAG, "Firing onSwipeFromCenterToDown:" + event.getPointerCount());
//判断三指开始截图
if (event.getPointerCount() == 3) {
sScreenshotHelper.takeScreenshot(WindowManager.TAKE_SCREENSHOT_FULLSCREEN, true, true, mScreenShotHandler, null);
}
}
}
break;
那么三指截图就完成了!!!!
是不是很干,没有水分,有用记得点赞收藏。
补充:
private static final int SWIPE_FROM_CENTER_TO_DOWN = 5;