Draw and save images for Android

Source: Internet
Author: User
Canvas is a Canvas. You can create a blank Canvas and create a new Canvas object without parameters. You can also use BitmapFactory to create a Bitmap object as a parameter for the new Canvas object. That is to say, the Canvas is not blank. If you want to save the image, it is better that Bitmap is a new one, instead of reading in from a file or a Drawable object. Use Canvas to draw the first image, draw the second image, and finally use Canvas. save (int flag) method. Note that the parameters in the save method can save a single layer. If you want to save all layers, use save (Canvas. ALL_SAVE_FLAG ). All the last information will be saved in the first created Bitmap. The Code is as follows: Java code
  1. /**
  2. * Create the bitmap from a byte array
  3. *
  4. * @ Param src the bitmap object you want proecss
  5. * @ Param watermark the water mark abve the src
  6. * @ Return a bitmap object, if paramter's length is 0, return null
  7. */
  8. Private Bitmap createBitmap (Bitmap src, Bitmap watermark)
  9. {
  10. String tag = "createBitmap ";
  11. Log. d (tag, "create a new bitmap ");
  12. If (src = null)
  13. {
  14. Return null;
  15. }
  16. Int w = src. getWidth ();
  17. Int h = src. getHeight ();
  18. Int ww = watermark. getWidth ();
  19. Int wh = watermark. getHeight ();
  20. // Create the new blank bitmap
  21. Bitmap newb = Bitmap. createBitmap (w, h, Config. ARGB_8888); // create a new Bitmap with the same length and width as SRC.
  22. Canvas cv = new Canvas (newb );
  23. // Draw src
  24. Cv. drawBitmap (src, 0, 0, null); // draw src at the coordinates of 0, 0.
  25. // Draw watermark
  26. Cv. drawBitmap (watermark, w-ww + 5, h-wh + 5, null); // Add a watermark to the bottom right corner of src.
  27. // Save all clip
  28. Cv. save (Canvas. ALL_SAVE_FLAG); // save
  29. // Store
  30. Cv. restore (); // Storage
  31. Return newb;
  32. }
Method for downgrading images: Java code
  1. /**
  2. * Lessen the bitmap
  3. *
  4. * @ Param src bitmap
  5. * @ Param destWidth the dest bitmap width
  6. * @ Param destHeigth
  7. * @ Return new bitmap if successful, oherwise null
  8. */
  9. Private Bitmap lessenBitmap (Bitmap src, int destWidth, int destHeigth)
  10. {
  11. String tag = "lessenBitmap ";
  12. If (src = null)
  13. {
  14. Return null;
  15. }
  16. Int w = src. getWidth (); // source file size
  17. Int h = src. getHeight ();
  18. // Calculate the scale-in this case = 0.4f
  19. Float scaleWidth = (float) destWidth)/w; // Width Reduction Ratio
  20. Float scaleHeight = (float) destHeigth)/h; // scale down the height
  21. Log. d (tag, "bitmap width is:" + w );
  22. Log. d (tag, "bitmap height is:" + h );
  23. Log. d (tag, "new width is:" + destWidth );
  24. Log. d (tag, "new height is:" + destHeigth );
  25. Log. d (tag, "scale width is:" + scaleWidth );
  26. Log. d (tag, "scale height is:" + scaleHeight );
  27. Matrix m = new Matrix (); // Matrix
  28. M. postScale (scaleWidth, scaleHeight); // sets the matrix ratio.
  29. Bitmap resizedBitmap = Bitmap. createBitmap (src, 0, 0, w, h, m, true); // directly draw the source file according to the matrix ratio.
  30. Return resizedBitmap;
  31. }
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.