我知道有很多关于截屏的问题,我已经检查了大部分。他们有相同的答案(代码变化很小)。
我有以下截屏方法:
@NonNull
public static Bitmap takeScreenShot(Window window) throws IOException {
final View rootView = window.getDecorView().getRootView();
final boolean drawingCacheEnabled = rootView.isDrawingCacheEnabled();
rootView.setDrawingCacheEnabled(true);
try {
return Bitmap.createBitmap(rootView.getDrawingCache());
} finally {
rootView.setDrawingCacheEnabled(drawingCacheEnabled);
}
}
您可以像这样使用它:takeScreenShot(getActivity().getWindow())
但是这些方法有几个局限性:
- 如果屏幕上有一些对话,它们将不会被捕获 截屏。
- 它是否适用于硬件加速 View ?根据
文档:
When hardware acceleration is turned on, enabling the
drawing cache has no effect on rendering because the system uses a
different mechanism for acceleration which ignores the flag - 屏幕截图包含黑框 而不是 GLviews。 (例如,当您的应用程序有 map 时。)。这似乎是第二点的结果。
所以我的问题是,是否有任何无需 root 的解决方案至少可以解决我的部分问题?
最佳答案
查看以下 GitHub 存储库(不是我的!):https://github.com/AndroidDeveloperLB/ScreenshotSample
此外,以下内容将是有用的阅读: How to properly take a screenshot, globally?
关于android - 如何截取屏幕截图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47345930/