Achieve the Android page snapshot feature

Source: Internet
Author: User

Now the general shopping site, after you complete the transaction will take pictures of the page to avoid future business disputes, and for our mobile developers this traditional Internet experience has also given us some design inspiration, then I will be several implementation ideas to write for your reference.

Scenario One: Using the Webviewclient onpagefinished event

When we use WebView as the Web page of the embedded browser in the program, if we do not make special settings, when the user clicks the link inside the WebView will launch the Android phone built-in browser, and leave the current activity, in response to this problem, We can customize the client settings for the browser, with the following code:

    1. Mwebview.setwebviewclient (new Webviewclient () {
    2. });

In the content of setwebviewclient (), we can rewrite onpagefinished () to capture WebView loaded events, the key code is as follows:

  1. / * WebView Get websettings * /
  2. WebSettings websettings = Wview.getsettings ();
  3. / * Set to run JavaScript * /
  4. Websettings.setjavascriptenabled (true);
  5. /*  */  
  6. /*  */  
  7. / * WebView Set webviewclient * /
  8. Wview.setwebviewclient (new Webviewclient () {
  9. @Override
  10. public void onpagefinished (WebView view, String URL) {
  11. / * Get the picture * /
  12. Picture picture = View.capturepicture ();
  13. Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
  14. / * Get width/height * /
  15. int width = picture.getwidth ();
  16. int height = picture.getheight ();
  17. if (Width > 0 && height > 0) {
  18. Bitmap Bitmap = bitmap.createbitmap (width, height, Bitmap.Config.ARGB_8888);
  19. Canvas canvas = new canvas (bitmap);
  20. Picture.draw (canvas);
  21. Bitmap.compress (Bitmap.CompressFormat.JPEG, BAOs);
  22. FileOutputStream FOS = null;
  23. try {
  24. if (Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {
  25. FOS = New FileOutputStream (Environment.getexternalstoragedirectory ());
  26. if (fos!=null) {
  27. Bitmap.compress (Bitmap.CompressFormat.JPEG, N, FOS);
  28. Fos.close ();
  29. Toast.maketext (atestactivity.   This, "snapshot saved successfully", Toast.length_long). Show ();
  30. }
  31. }
  32. } catch (Exception e) {
  33. E.printstacktrace ();
  34. }
  35. }
  36. super.onpagefinished (view, URL);
  37. }
  38. });


Scenario Two:

Using the ViewThe Bitmap getdrawingcached (Boolean Autoscale) method is implemented, but it is important to note thatThe cache must first be opened by the Setdrawingcacheenable method, and then call the Getdrawingcache method to get the cache image of the view. The Builddrawingcache method can not be called, because when the Getdrawingcache method is called, the system automatically calls the Builddrawingcache method to generate the cache if the cache is not established. If you want to update the cache, you must call the Destorydrawingcache method to destroy the old cache to create a new.
When the call setdrawingcacheenabled method is set to False, the system will automatically destroy the original cache.
ViewGroup provides two ways to create a sub-view
void Setchildrendrawingcacheenabled (Boolean enabled)
Setchildrendrawnwithcacheenabled (Boolean enabled)
The Setchildrendrawingcacheenabled method enables all sub-view in the ViewGroup to open the cache, setchildrendrawnwithcacheenabled so that when the child view is drawn, If the child view opens the cache, it is drawn using its cache, which saves drawing time.
getting the cache usually consumes a certain amount of memory, so it is often necessary to clean it up when it is not needed, by Destroydrawingcache or setdrawingcacheenabled (false).

    1. Public static Bitmap convertviewtobitmap (view view) {
    2. View.builddrawingcache ();
    3. Bitmap Bitmap = View.getdrawingcache ();
    4.     return bitmap;
    5. }

In general, direct use of the above method is able to work normally. Sometimes, however, there is a problem with generating bitmap (bitmap full black). The main reason is that the value of Drawingcache is greater than the value given by the system. We can take a look at some of the code in the Builddrawingcache () method:

    1. if (width <= 0 | | height <= 0 | | (Width * Height * (opaque &&!translucentwindow?) 2: 4) > Viewconfiguration.get (mcontext). Getscaledmaximumdrawingcachesize ())) {     
    2. Destroydrawingcache ();
    3. return;
    4. }

In the above code, width and height are the widths and heights of the view drawing for the cache, so (width * height * (opaque &&!translucentwindow 2:4) calculates the current required CAC He size. Viewconfiguration.get (Mcontext). Getscaledmaximumdrawingcachesize () Gets the maximum Drawingcache value provided by the system. When the required Drawingcache > system provides the maximum Drawingcache value, a problem occurs when the bitmap is generated, and the bitmap obtained is null.

So you can fix the problem by simply modifying the desired cache value. The code is implemented as follows:

    1. Public static Bitmap convertviewtobitmap (view view) {
    2. View.measure (Measurespec.makemeasurespec (0, measurespec.unspecified), Measurespec.makemeasurespec (0,  measurespec.unspecified));
    3. View.layout (0, 0, View.getmeasuredwidth (), View.getmeasuredheight ());
    4. View.builddrawingcache ();
    5. Bitmap Bitmap = View.getdrawingcache ();
    6. return bitmap;
    7. }

Achieve the Android page snapshot feature

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.