android - 如何截取屏幕截图?

标签 android screenshot

我知道有很多关于截屏的问题,我已经检查了大部分。他们有相同的答案(代码变化很小)。

我有以下截屏方法:

@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())

但是这些方法有几个局限性:

  1. 如果屏幕上有一些对话,它们将不会被捕获 截屏。
  2. 它是否适用于硬件加速 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

  3. 屏幕截图包含黑框 而不是 GLviews。 (例如,当您的应用程序有 map 时。)。这似乎是第二点的结果。

所以我的问题是,是否有任何无需 root 的解决方案至少可以解决我的部分问题?

最佳答案

查看以下 GitHub 存储库(不是我的!):https://github.com/AndroidDeveloperLB/ScreenshotSample

此外,以下内容将是有用的阅读: How to properly take a screenshot, globally?

关于android - 如何截取屏幕截图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47345930/

相关文章:

android - 持续更新UI的后台服务

Android Studio,Windows : ADB not responding. 如果您想重试,请手动杀死 "adb.exe"并单击 'Restart'

iOS7 截屏预防/检测

php - 如何获取使用 php/JavaScript 上传的文档第一页的屏幕截图

android - 使用 ddmlib.jar 获取 Android 设备方向

selenium - 如何使用 Xvfb 和 Selenium 拍摄 Retina 屏幕截图

android-layout - Android layout-sw600dp 不适用于 8"Galaxy Tab

android - 将像素转换为 sp

android - 如何通过 Google Cloud Endpoint API (Android) 在 Google Datastore 上获取和设置对象

html - 如何在我的浏览器新标签显示的最近使用的网站列表中控制我网站的截图