How to use onDraw(Canvas) to obtain a bitmap snaps

2019-05-14 09:13发布

问题:

I used to use capturePicture() method to make a snapshot of my WebView. This method was deprecated in API level 19.

The doc say that "Use onDraw(Canvas) to obtain a bitmap snapshot of the WebView", but I really don't know how it means.

Will you please teach me how to solve the problem?

回答1:

The following should work:

float scale = webView.getScale();
int webViewHeight = (int)(webView.getContentHeight() * scale);
Bitmap image = Bitmap.createBitmap(webView.getWidth(), webViewHeight, Config.ARGB_8888);
Canvas canvas = new Canvas(image);
webView.draw(canvas);