Android View Conversion Bitmap

Source: Internet
Author: User

The parameters of the OnDraw method in the View class canvas is the background of the view drawing, and to convert the view to bitmap is actually to draw the drawing operations on the canvas to bitmap.


View conversion to bitmap is also known as a screenshot, which allows the user to see the view converted into a picture process.


The methods in the view class related to view conversion bitmap are:


protected void OnDraw (canvas canvas) public void Builddrawingcache () public void Destroydrawingcache () public Bitmap Getdrawingcache () public void setdrawingcacheenabled (Boolean enabled)


Here are a few examples of common view screenshots:

1.View Turn bitmap

Public final bitmap screenshot (View view)  {         if  (Null == view)  {             throw new illegalargumentexception ("Parameter can ' T be null.");         }        view.measure ( view.measurespec.unspecified, view.measurespec.unspecified);         view.layout (0, 0, view.getmeasuredwidth (),  view.getmeasuredheight ());         view.setdrawingcacheenabled (True);         view.builddrawingcache ();        bitmap bitmap =  View.getdrawingcache ();        return bitmap;     }


2. Activity transfer bitmap without status bar

Public final bitmap screenshot (activity activity)  {         if  (null == activity)  {             throw new illegalargumentexception ("Parameter can ' T be null.");         }        View  View = activity.getwindow (). Getdecorview ();         View.setdrawingcacheenabled (True);         view.builddrawingcache ();         bitmap b1 = view.getdrawingcache ();         rect frame = new rect ();         view.getwindowvisibledisplayframe (frame);         int  statusbarheight = frame.top;        point point = new point ();         activity.getwindowmanager (). Getdefaultdisplay (). GetSize (Point) ;        int width = point.x;         int height = point.y;         Bitmap b2 = bitmap.createbitmap (b1, 0, statusbarheight, width, height  - statusbarheight);         view.destroydrawingcache ();         return b2;    }


3. ScrollView to Long bitmap (similar to the length of the hammer note)


 public final bitmap screenshot (Scrollview scrollview)  {         if  (Null == scrollview)  {             throw new illegalargumentexception ("Parameter can ' T be  null. ");         }        int  height = 0;        bitmap bitmap;         for  (Int i = 0, s = scrollview.getchildcount ( );  i < s; i++)  {             height += scrollview.getchildat (i). GetHeight ();             scrollview.getchildat (i). Setbackgroundresource (Android. R.drawable.screen_background_light);         }        bitmap =  bitmap.createbitmap (Scrollview.getwidth (),  height, bitmap.config.argb_8888);         final canvas canvas = new canvas (bitmap);         scrollview.draw (Canvas);         return bitmap;    }

This article is from the "Red Horse Red" blog, please be sure to keep this source http://aiilive.blog.51cto.com/1925756/1711443

Android View Conversion Bitmap

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.