Canvas after drawing save picture, in two cases, one: your own new Canvas object. The second type: Introducing the OnDraw function as a parameter to the canvas
First case:
Create an empty bitmap object, and then, when you initialize the canvas, bring it in as a parameter, and then the canvas does the drawing operation on that bitmap, as long as the bitmap object is saved. The following code: Draw a blue ring:
//this way, just use canvas and saveBitmap Bitmap = Bitmap.createbitmap (700,700, Bitmap.Config.ARGB_8888); Canvas Canvas=NewCanvas (bitmap); Canvas.drawcolor (color.white);Paint Paint=NewPaint (); Paint.setantialias (true);//set anti-aliasingPaint.setstrokewidth (7);//Set Stroke widthPaint.setstyle (Paint.Style.STROKE); //Five Olympic rings for paintingPaint.setcolor (Color.Blue); Canvas.drawcircle (150,150,100,paint);//draw a blue circleCanvas.save (canvas.all_save_flag);//SaveCanvas.restore ();//StorageFile Sdroot=environment.getexternalstoragedirectory (); String Save_path= Sdroot.getabsolutepath () + "/dcim/100media/114.jpg"; Try{bitmap.compress (Bitmap.CompressFormat.JPEG,100,NewFileOutputStream (NewFile (Save_path)); } Catch(FileNotFoundException e) {e.printstacktrace (); }
Second case:
The point is setdrawingcacheenabled (true); and Getdrawingcache () these two functions
Public MyView (Context context) { Super(context); // This function is to open the graphics cache so that you can getdrawingcache Setdrawingcacheenabled (true); }
protected voidOnDraw (canvas canvas) {canvas.drawcolor (color.white); Paint Paint=NewPaint (); Paint.setantialias (true);//set anti-aliasingPaint.setstrokewidth (7);//Set Stroke widthPaint.setstyle (Paint.Style.STROKE); //Draw a ringPaint.setcolor (Color.Blue); Canvas.drawcircle (150,150,100,paint);//draw a blue circleCanvas.save (canvas.all_save_flag);//SaveCanvas.restore ();//Storage//Save the draw in the canvasFile Sdroot =environment.getexternalstoragedirectory (); String Save_path= Sdroot.getabsolutepath () + "/dcim/100media/113.jpg"; Try{Getdrawingcache (). Compress (Bitmap.CompressFormat.JPEG,100,NewFileOutputStream (NewFile (Save_path)); } Catch(Exception e) {LOG.E ("Error--------->", e.tostring ()); } Super. OnDraw (canvas); }
Save a picture after android+ canvas drawing