Android Paint and save the picture's specific implementation code _android

Source: Internet
Author: User
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 shrink 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 proportions
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.