android - 在android中以编程方式截取屏幕截图

标签 android screenshot

我正在使用以下代码以编程方式截取屏幕截图:

public static Bitmap takeScreenshot(View view)
    {
        try
        {
            // create bitmap screen capture
            view.setDrawingCacheEnabled(true);
            Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
            view.setDrawingCacheEnabled(false);
            return bitmap;
        }
        catch (Throwable e)
        {
            CustomLogHandler.printError(e);
        }
        return null;
    }

private static void copyFile(Bitmap bitmap)
    {
        File dstFile = getShareResultFile();

        //Delete old file if exist.
        if(dstFile.exists()) {
            dstFile.delete();
        }

        FileOutputStream fos = null;
        try
        {
            fos = new FileOutputStream(dstFile);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 0, fos);
            fos.flush();
        }
        catch (Exception e) {
            CustomLogHandler.printError(e);
        }
        finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException ioe) {
                    CustomLogHandler.printError(ioe);
                }
            }
        }
    }

有几个问题,比如:

  1. 后退箭头、标题和共享菜单的背景颜色不正确。看起来很乱。
  2. 工具栏的背景颜色完全改变。
  3. 图像质量太差,列表项圆形可绘制对象的角不平滑。
  4. 未采用我设置为父布局背景的布局背景。

我正在从 Root View 截屏。

ma

最佳答案

bitmap.compress(Bitmap.CompressFormat.JPEG, 0, fos);

首先,您将其保存为 JPEG。 JPEG 专为照片而设计,您的屏幕截图不是照片。

其次,您以质量因数 0 保存此图像。JPEG 使用有损压缩算法,质量因数 0 表示“请随意使此图像非常差,但尽可能压缩它".

我建议切换到:

bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);

PNG 是一种更好的屏幕截图图像格式,其中包含您问题中显示的内容。我不认为 PNG 使用质量因子值;我输入 100 只是为了表明您想要最好的质量。

关于android - 在android中以编程方式截取屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55829773/

相关文章:

Android,存储等待网络连接的http请求的解决方案/库

java - 正则表达式仅阻止数字或空格,但允许数字和空格作为输入

android - 如何让 Spinner 项目在被点击时以全宽显示在自身下方,就像在 G+ 应用程序上一样

由于内存峰值导致截屏时 ios 应用程序崩溃

c++ - 在 Qt 中使用 QDesktopWidget 截取多个屏幕的屏幕截图

java - 如何使 Cucumber 功能作为本地单元测试在 Android 项目中运行?

android - react native : How to maintain multiple state of a component

ios - 如何在ios9中捕获 View 的完整屏幕截图

android - 有没有办法在eclipse中自动化模拟器android

screenshot - 有没有办法截取整个 HTML 页面(包括首屏内容)的屏幕截图?