作者:我不是咸鱼仔 | 来源:互联网 | 2023-08-22 19:21
frameworks\base\packages\SystemUI\src\com\android\systemui\screenshottakeScreenshot方法Wenee
frameworks\base\packages\SystemUI\src\com\android\systemui\screenshot
takeScreenshot方法
// We need to orient the screenshot correctly (and the Surface api seems to take screenshots// only in the natural orientation of the device :!)mDisplay.getRealMetrics(mDisplayMetrics);float[] dims = {mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels};float degrees = getDegreesForRotation(mDisplay.getRotation());boolean requiresRotation = (degrees > 0);if (requiresRotation) {// Get the dimensions of the device in its native orientationmDisplayMatrix.reset();mDisplayMatrix.preRotate(-degrees);mDisplayMatrix.mapPoints(dims);dims[0] = Math.abs(dims[0]);dims[1] = Math.abs(dims[1]);}// Take the screenshotmScreenBitmap = SurfaceControl.screenshot((int) dims[0], (int) dims[1]);if (mScreenBitmap == null) {notifyScreenshotError(mContext, mNotificationManager);finisher.run();return;}if (requiresRotation) {// Rotate the screenshot to the current orientationBitmap ss = Bitmap.createBitmap(mDisplayMetrics.widthPixels,mDisplayMetrics.heightPixels, Bitmap.Config.ARGB_8888);Canvas c = new Canvas(ss);c.translate(ss.getWidth() / 2, ss.getHeight() / 2);c.rotate(degrees);c.translate(-dims[0] / 2, -dims[1] / 2);c.drawBitmap(mScreenBitmap, 0, 0, null);c.setBitmap(null);// Recycle the previous bitmapmScreenBitmap.recycle();mScreenBitmap = ss;}// OptimizationsmScreenBitmap.setHasAlpha(false);mScreenBitmap.prepareToDraw();// Start the post-screenshot animationstartAnimation(finisher, mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels,statusBarVisible, navBarVisible);
// Take the screenshot
mScreenBitmap = SurfaceControl.screenshot((int) dims[0], (int) dims[1]);
主要还是这一句也就是SurfaceControl.screenshot();
所以我们也可以调用反射调用
//使用反射调用截屏private void screenShotByReflect(){DisplayMetrics mDisplayMetrics = new DisplayMetrics();float[] dims = { mDisplayMetrics.widthPixels,mDisplayMetrics.heightPixels };try {Class> demo = Class.forName("android.view.SurfaceControl");Method method = demo.getDeclaredMethod("screenshot", int.class,int.class);mScreenBitmap = (Bitmap) method.invoke(null,(int) dims[0],(int) dims[1]);} catch (Exception e) {e.printStackTrace();}}
因为这部分代码是在setting里的,可能是需要系统签名的,我自己试了一下,没有系统签名是不成功的。