Recently, I have always seen many APPs that support long screenshots of articles and web pages. I have studied them out of curiosity and share them with you.
There are many examples on the Internet, many of which are outdated and will not be repeated. I found that there is a more general method.
//After Android 5.0, you need to enable the browser's overall cache to intercept the entire webif (.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { (); }
First, you need to call the enableSlowWholeDocumentDraw() method of the WebView before the initialization of the WebView (usually before the setContentView() method is called), and enable the WebView's Html cache. Because after Android 5.0, in order to improve performance, a document that only renders the currently visible Html document has been added to the WebView. So if this function is not enabled, we can only get screenshots of some web pages when taking screenshots.
Notice:Turning on this feature will increase performance overhead.
I'll post the code for the screenshot first:
public static boolean getFullWebViewSnapshot(WebView webView, String savePath) { //Recall the measure method of the WebView to measure the size of the actual View (set the measurement mode to UNSPECIFIED mode, which means you can get as much space as you need) ((, ), (0, )); //Call the layout method to set the layout (using the newly measured size) (0, 0, (), ()); //Open the cache of the WebView (when this switch is turned on, the view will be drawn on a bitmap next time you call the getDrawingCache() method) (true); //Forcibly draw the cache (it must be called after setDrawingCacheEnabled(true), otherwise you need to call destroyDrawingCache() manually to clearly cache) (); //Create a bitmap of the same size based on the measurement results Bitmap picture = ((), (), .ARGB_8888); //I have created a canvas for the background picture Canvas canvas = new Canvas(picture); // The width and height of the canvas are consistent with the web page of the WebView Paint paint = new Paint(); //Set the fixed point position of the brush, that is, the upper left corner (picture, 0, (), paint); //Draw the webview on the artboard you just created (canvas); try { //Save bitmap to SD card (picture, savePath); return true; } catch (IOException e) { (); return false; } }
It's actually very simple, the idea is as follows:
- Remeasure the entire size of the webview through the measure method of the WebView;
- Create a bitmap with the same size as the real size of the webview;
- Draw the webview onto the bitmap;
- Save bitmap to the SD card;
This basically realizes the overall screenshot function of a WebView. Of course it is not very complete, so I will add it if I have time.
This is the article about the complete screenshot of the Android control WebView implementation. For more information about the complete screenshot of the Android implementation, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!