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