我正在使用以下代码以编程方式截取屏幕截图:
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);
}
}
}
}
有几个问题,比如:
- 后退箭头、标题和共享菜单的背景颜色不正确。看起来很乱。
- 工具栏的背景颜色完全改变。
- 图像质量太差,列表项圆形可绘制对象的角不平滑。
- 未采用我设置为父布局背景的布局背景。
我正在从 Root View 截屏。
最佳答案
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/