Android 中怎么利用WebView 实现截图

Android 中怎么利用WebView 实现截图,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

WebView 作为一种特殊的控件,自然不能像其他系统 View 或者截屏的方式来获取截图(多为截取长图)。如:

public static Bitmap getScreenShot(View view){
  View screenView = view.getRootView();
  screenView.setDrawingCacheEnabled(true);
  Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
  screenView.setDrawingCacheEnabled(false);
  return bitmap;
}

如果将上述代码套在 WebView 上使用,将会得到内容不完整的截图。而事实上,WebView 系统本身提供有对应的 API 来获取 Bitmap 对象。

private Bitmap captureWebView(WebView webView){
 Picture picture = webView.capturePicture();
 int width = picture.getWidth();
 int height = picture.getHeight();
 if (width > 0 && height > 0) {
  Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
  Canvas canvas = new Canvas(bitmap);
  picture.draw(canvas);
  return bitmap;
 }
 return null;
}

获取到 Bitmap 对象后,利用这段代码可以将其保存到设备的存储卡中:

private void saveBitmap(Bitmap bitmap){
 File file = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg");
 try {
  FileOutputStream fos = new FileOutputStream(file);
  bitmap.compress(CompressFormat.JPEG, 80, fos);
  fos.flush();
  fos.close();
 } catch (java.io.IOException e) {
  e.printStackTrace();
 }
}

简单两步,大功告成。然而当你在 Android 5.0 及更高版本系统的设备中操作时,你会发现,截图显示并不完全。虽然图片宽高符合实际要求,但是内容只包含当前屏幕显示区域内 WebView 的内容。

原因在于,为了减少内存占用和提升性能,从 Android 5.0 开始,系统能够智能化地选择部分 Html 文档进行渲染。所以,默认情况下,我们只能截取到部分屏幕显示区域内 WebView 的内容,也就出现了上述问题。

不过,系统也提供了对应的 API 来修改这一默认优化行为。代码很简单:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
 WebView.enableSlowWholeDocumentDraw();
}

需要注意的是,这段代码必须添加在 WebView 实例被创建之前。如果使用 Activity 的话,也就是在 setContentView() 方法前面。

虽然 capturePicture() 方法已经能够获取 WebView 截图,但是到 API 19 时该方法被系统废弃掉了。取而代之的是使用 onDraw() 方法获取获取 Bitmap 对象。

private Bitmap captureWebView(WebView webView){
 float scale = webView.getScale();
 int width = webView.getWidth();
 int height = (int) (webView.getHeight() * scale);
 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
 Canvas canvas = new Canvas(bitmap);
 webView.draw(canvas);
 return bitmap;
}

这里又要提到的是, getScale() 方法从 API 17 开始也被系统废弃掉了。所以获取 scale 值的另一种更优雅的方式是:

webView.setWebViewClient(new WebViewClient() {
 @Override
 public void onScaleChanged(WebView view,float oldScale, float newScale){
  super.onScaleChanged(view, oldScale, newScale);
  scale = newScale;
 }
});

关于Android 中怎么利用WebView 实现截图问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注行业资讯频道了解更多相关知识。