In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
How to gracefully take a screenshot of Webview, I believe many inexperienced people are at a loss about it. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
How to take a screenshot of Webview gracefully? The first way
By calling webview.capturePicture (), you get a picture object, create a Bitmap based on the width and height of the image, create a canvas, bind bitmap, and finally draw with picture.
In this way, you can get all the data images loaded in webview, that is, the effect of long screenshots. This approach is fine below Android 4.4, but not above 5. 0. The capturePicture () method was deprecated in 4.4, and officials recommend using the onDrow () method to take a bitmap snapshot of the webview. The specific implementation is as follows:
But at this point, on 5.0 +, you will find that the snapshot taken shows only the part shown in the webview, and the part that is not shown is blank. The reason is found through google, and in version 5.0 +, Android optimizes webview to reduce memory footprint and improve performance. Therefore, by default, it will intelligently draw the part of the html that needs to be drawn, which is actually the html content displayed on the current screen, so the undisplayed image will be blank. The workaround is to call the enableSlowWholeDocumentDraw () method. This method needs to be called before the webview is created, that is, before the setContentView is called in Activity, and this method has significant performance overhead. It is important to note that when passing the height of the webview, it is calculated by the scaling rate, which calculates the height required to draw the entire loaded html content. Without this scaling rate, the snapshot you get is only the top segment of the html content. Another problem is that getting snapshots on the 5.0 + system is blurry, and there is no problem in other versions. I don't know why.
The second way
Take advantage of the caching function of view. In order to improve the rendering speed of scrolling and other aspects, Android can establish a cache for each view and use View.buildDrawingCache to establish a corresponding cache for its own view. This cache is a bitmap object. Using this feature, you can take a screenshot of the entire screen view and generate Bitmap, and you can also get the Bitmap object of the specified view. So for webview, you can also use this method to turn on the caching function of webview before using the getDrawingCache () method to get the bitmap object.
It should be noted that in the above case, there is only one cached bitmap object, so each bitmap obtained points to a cached object in the same address space. If you recycle this object immediately after using the bitmap, you will get null when you get the cached object of the current view again. Therefore, it is necessary to recycle when the Activity is destroyed, so there is a performance overhead if the cache is turned on.
The third way
It is relatively simple by getting the DecorView of the current window and then drawing the Bitmap object.
After reading the above, have you mastered how to take a screenshot of Webview gracefully? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.