In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will share with you the relevant knowledge points about how to use WebView to achieve screenshots in Android. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.
As a special control, WebView cannot take screenshots like other system View or screenshots (mostly long screenshots). Such as:
Public static Bitmap getScreenShot (View view) {View screenView = view.getRootView (); screenView.setDrawingCacheEnabled (true); Bitmap bitmap = Bitmap.createBitmap (screenView.getDrawingCache ()); screenView.setDrawingCacheEnabled (false); return bitmap;}
If you use the above code on WebView, you will get a screenshot of the incomplete content. In fact, the WebView system itself provides a corresponding API to get the Bitmap object.
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;}
After getting the Bitmap object, you can use this code to save it to the storage card of the device:
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 ();}}
Two simple steps, and it's done. However, when you operate on Android 5.0 and later devices, you will find that the screenshot is not complete. Although the width and height of the picture meet the actual requirements, the content only contains the content of WebView in the current screen display area.
The reason is that, in order to reduce memory consumption and improve performance, the system can intelligently select some Html documents for rendering since Android 5.0. Therefore, by default, we can only intercept the contents of WebView in part of the display area of the screen.
However, the system also provides a corresponding API to modify this default optimization behavior. The code is simple:
If (Build.VERSION.SDK_INT > = Build.VERSION_CODES.LOLLIPOP) {WebView.enableSlowWholeDocumentDraw ();}
It is important to note that this code must be added before the WebView instance is created. If you use Activity, that's before the setContentView () method.
Although the capturePicture () method was able to take WebView screenshots, it was abandoned by the system by 19:00 in API. Instead, use the onDraw () method to get the Bitmap object.
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;}
Again, the getScale () method has been deprecated by the system since API 17. So another more elegant way to get the scale value is:
WebView.setWebViewClient (new WebViewClient () {@ Override public void onScaleChanged (WebView view,float oldScale, float newScale) {super.onScaleChanged (view, oldScale, newScale); scale = newScale;}); that's all about the article "how to use WebView to capture screenshots in Android". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.