Save a picture after android+ canvas drawing

Source: Internet
Author: User

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

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.