Two ways to use the screenshot of Android

Source: Internet
Author: User

During the Android app development process, you may encounter the need for an entire interface or part. There are also many ways to view in Android:

    1. Using Drawingcache
    2. Call View.draw directly
Drawingcache Method:If you use Drawingcache, there is a requirement for the view you want: The view itself is already displayed on the interface. If the view is not added to the interface or is not displayed (drawn), the Builddrawingcache will fail. This method is more suitable for the application interface or some part of the. The steps are simple:
View.setdrawingcacheenabled (True); View.builddrawingcache ();  Enable Drawingcache and create bitmap bitmap bitmap = Bitmap.createbitmap (View.getdrawingcache ()); Create a copy of the Drawingcache because the bitmap Drawingcache obtained will be reclaimed view.setdrawingcacheenabled (false) when it is disabled;  Disabling DRAWINGCAHCE otherwise will affect performance

View.draw Method:If the desired view is not added to the interface, it may have been created by Java code or inflate, and the Drawingcache method is called to get the bitmap. Because view does not get the actual size before it is added to the container (if Layoutwidth is matchparent, it has no parent ...). ), so you first need to specify the size of the view:
private void Layoutview (View v, int width, int height) {//Validate view.width and view.heightv.layout (0, 0, width, height int measuredwidth = View.MeasureSpec.makeMeasureSpec (width, View.MeasureSpec.EXACTLY); int measuredheight = View.MeasureSpec.makeMeasureSpec (height, View.MeasureSpec.EXACTLY);//Validate View.measurewidth and View.measureheightv.measure (Measuredwidth, measuredheight); v.layout (0, 0, v.getmeasuredwidth (), V.getmeasuredheight ()); I}

Then the view and its inner sub-view have the actual size, that is, the layout has been completed, quite and added to the interface. You can then create the bitmap and draw it on it:
int viewwidth = Webview.getmeasuredwidth (), int viewheight = Webview.getmeasuredheight (), if (viewwidth > 0 && V Iewheight > 0) {b = Bitmap.createbitmap (Viewwidth, Viewheight, config.argb_8888); Canvas CVS = new canvas (b); Webview.draw (CVS);}

For WebView a bit special, the page content can not be rendered immediately after the layout is complete, because the webview size changes quite with the desktop browser window size changes, WebKit need to re-render all the content according to the window size, This can take up to 300ms of time (different for different performance devices, Web page complexity, and WebKit versions). If you need to create a background webview, you should lay it out when you create it so that most of the loading is finished (unless you have asynchronous JS processing).

Two methods of Android application

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.