Android screenshot, how to change the screenshot content, the two bitmap merged together to display the image

Source: Internet
Author: User

The requirements that are encountered in the project, need, and methods are as follows

This method passes in a desired activity

/** *  to the corresponding activity *  * @param activity * @return */public Bitmap takescreenshot (activity activity) {View Rootview = Activity.getwindow (). Getdecorview (); rootview.setdrawingcacheenabled (true); Rootview.builddrawingcache (); Bitmap Bitmap = Rootview.getdrawingcache ();//Gets the status bar height Rect frame = new Rect (); Activity.getwindow (). Getdecorview (). Getwindowvisibledisplayframe (frame); int statusbarheight = frame.top;//log.i ("TAG", "" + statusbarheight);// Gets the screen length and height int width = activity.getwindowmanager (). Getdefaultdisplay (). getwidth (); int height = Activity.getwindowmanager (). Getdefaultdisplay (). GetHeight ();//Remove title bar Bitmap B = Bitmap.createbitmap (Bitmap, 0, Statusbarheight, Width,height- STATUSBARHEIGHT-HEIGHT/3 + +); rootview.setdrawingcacheenabled (false); return b;}

But found that some of the captured images need my modification, for example, want to replace the title in another, the original activity can not be modified, can only re-make a bitmap, and add this bitmap to the corresponding position of the bitmap returned above The following method is added:

<span style= "White-space:pre" ></span>//This is the length that I have defined from the resource file, and can also be set freely
<span style= "White-space:pre" >float dimension = Getresources (). Getdimension (<span style= "White-space:pre" ></span>r.dimen.activity_action_bar_height);</span>
</pre><pre name= "code" class= "Java" >
<span style= "White-space:pre" ></span><pre name= "code" class= "java" style= "Font-size: 13.3333339691162px; " ><span style= "White-space:pre" ></span>//conview for the view I want to add is the one I wrote before, the view used to generate the replacement bitmap<span> </span>conview.setdrawingcacheenabled (True); <span></span>conview.builddrawingcache ();< Span></span><span></span>bitmap bitmap3 = Conview.getdrawingcache ();//This method needs to be noted, and down will say <span ></span>bitmap bitmap2 = Bitmap.createbitmap (bitmap3, 0, Statusbarheight,<span></span>width, ( int) dimension);
int w = bitmap2.getwidth (); int h = bitmap2.getheight (); int[] pixels = new INT[W * h];bitmap2.getpixels (pixels, 0, W, 0, 0, W, h);
<span style= "White-space:pre" ></span>bitmap B = Bitmap.createbitmap (Bitmap, 0, statusbarheight, width, HEIGHT-STATUSBARHEIGHT-HEIGHT/3 + 100);//
<span style= "White-space:pre" ></span>//replaces the generated bitmap with the original bitmap position b.setpixels (pixels, 0, W, WIDTH/5, 0, W, h );


Run results found error, the view generated by their own bitmap is empty, that is, bitmap bitmap3 = Conview.getdrawingcache (), return empty, originally calculated Conview bitmap exceeded the size of the system memory limit, 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:

<span style= "LINE-HEIGHT:19.5PX; Color:rgb (75, 75, 75); Font-family:verdana, Geneva, Arial, Helvetica, Sans-serif; font-size:13px; " ><span style= "line-height:1.5;" ></span></span><pre style= "margin-top:0px; margin-bottom:0px; White-space:pre-wrap; Word-wrap:break-word; font-size:12px; line-height:18px; font-family: ' Courier New '!important; ><span style= "Color:rgb (0, 0, 255); line-height:1.5!important; FONT-SIZE:12PX!important; " >if</span> (width <= 0 | | height <= 0 | | (Width * Height * (opaque &&!translucentwindow 2:4) ><span style= "line-height:1.5!important; font-siz E:12PX!important; "                    > Viewconfiguration.get (mcontext). Getscaledmaximumdrawingcachesize ()) {Destroydrawingcache (); </span><span style= "Color:rgb (0, 0, 255); line-height:1.5!important; FONT-SIZE:12PX!important; " >return</span><span style= "line-height:1.5!important; Font-size: 12px!important; "     >; } </span>
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. So we introduced the second method:
public static Bitmap Convertviewtobitmap (view view) {
View.measure (Measurespec.makemeasurespec (0, measurespec.unspecified), Measurespec.makemeasurespec (0, measurespec.unspecified));
View.layout (0, 0, view.getmeasuredwidth (), View.getmeasuredheight ());
View.builddrawingcache ();
Bitmap Bitmap = View.getdrawingcache ();


return bitmap;
}


<span style= "LINE-HEIGHT:19.5PX; Color:rgb (75, 75, 75); Font-family:verdana, Geneva, Arial, Helvetica, Sans-serif; font-size:13px; " ><span style= "line-height:1.5;" ></span></span>
<span style= "LINE-HEIGHT:19.5PX; Color:rgb (75, 75, 75); Font-family:verdana, Geneva, Arial, Helvetica, Sans-serif; font-size:13px; " ><span style= "line-height:1.5;" > This method replaces the previous method of generating bitmap with view </span></span>


Android screen, how to change the content, merge two bitmap together to display the image

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.