Android Paint and save the picture's specific implementation code

Source: Internet
Author: User
This article introduces the example of drawing and saving pictures in Android, the following is the specific implementation method, a friend in need can refer to the

Canvas is a canvas in which you can create a blank canvas, directly new to a canvas object, without the need for parameters.
You can also use Bitmapfactory to create a bitmap object as a parameter to the new canvas object, which means that the canvas is not blank,
If you want to save the picture, it's best to bitmap is a new one, not read in from a file or Drawable object.

Then use canvas to draw the first picture up, in the second picture, and finally use the Canvas.save (int flag) method to save, note that the Save method parameters can save a single layer,
Use Save (Canvas.all_save_flag) If you are saving all layers.

Finally, all the information is saved in the first created bitmap. The code is as follows:
Java code

Copy Code code as follows:


/**


* Create the bitmap from a byte array


    *


* @param src the Bitmap object you want Proecss


* @param watermark The water mark above the SRC


* @return Return a Bitmap object, if paramter ' s length is 0,return null


    */ 


Private Bitmap CreateBitmap (Bitmap src, Bitmap watermark)


   { 


String tag = "CreateBitmap";


LOG.D (Tag, "Create a new bitmap");


if (src = null)


       { 


return null;


       } 





int w = src.getwidth ();


int h = src.getheight ();


int ww = watermark.getwidth ();


int wh = Watermark.getheight ();


Create the new blank bitmap


Bitmap newb = Bitmap.createbitmap (W, H, config.argb_8888);//Create a new bitmap with the same width as src length


Canvas CV = new Canvas (NEWB);


//draw src into


cv.drawbitmap (src, 0, 0, NULL);//at 0, 0 coordinates begin to draw into Src


//draw Watermark into


Cv.drawbitmap (watermark, W-WW + 5, H-WH + 5, NULL);//watermark in the lower right corner of SRC


//save All Clip


Cv.save (Canvas.all_save_flag);//Save


//store


Cv.restore ();//Storage


return newb;


   } 


How to zoom out on a picture:
Java code

Copy Code code as follows:




/**


* Lessen the bitmap


    *


* @param src bitmap


* @param destwidth the dest bitmap width


* @param destheigth


* @return New bitmap If successful, oherwise null


    */ 


private Bitmap Lessenbitmap (Bitmap src, int destwidth, int destheigth)


   { 


String tag = "Lessenbitmap";


if (src = null)


       { 


return null;


       } 


int w = src.getwidth ()//source file size


int h = src.getheight ();


//Calculate the scale-in this case = 0.4f


Float ScaleWidth = ((float) destwidth)/w;//width reduction ratio


Float ScaleHeight = ((float) destheigth)/h;//height reduction ratio


LOG.D (Tag, "Bitmap width is:" + W);


LOG.D (Tag, "bitmap height is:" + h);


LOG.D (Tag, "New width is:" + destwidth);


LOG.D (Tag, "New height is:" + destheigth);


LOG.D (Tag, "scale width is:" + scalewidth);


LOG.D (tag, "scale height is:" + scaleheight);


matrix M = new Matrix ();//matrices


M.postscale (ScaleWidth, ScaleHeight)//set matrix proportional


Bitmap resizedbitmap = bitmap.createbitmap (src, 0, 0, W, H, M, true);//directly draw the source file in the proportion of the matrix


return resizedbitmap;


   }

Related Article

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.