如何给当前的webview截屏

2025-04-27 18:10:36
推荐回答(1个)
回答1:

都知道android的webview有缩放状态,这样给当前屏幕截屏时可能只截到当前屏幕大小。

下面如何截取全屏。见代码:

float i = mWebView.getScale();
int h = (int)(mWebView.getContentHeight() * i);
final Bitmap bitmap = Bitmap.createBitmap(mWebView.getWidth(), h,
Bitmap.Config.ARGB_8888);
final Canvas c = new Canvas(bitmap);
mWebView.draw(c);

ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
final byte[] picture = stream.toByteArray();

if (bitmap != null && !bitmap.isRecycled()) {
bitmap.recycle();
}

此时当前网页的图片被转化为byte[]可以随你所用················了······