Android Learning notes Advanced 18 drawing and saving pictures to local

Source: Internet
Author: User

1, first create a bitmap picture, and specify the size;

2. Create a new canvas on the picture, then draw it on the canvas and save it;

3, need to save the directory file, note if the write directory such as "/sdcard/song/" if not exist, you must first create (File.mkdirs).


4, need to add permissions: <uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>

Canvas is a canvas, you can create a blank canvas, just new one canvas object, no parameters required. You can also use Bitmapfactory to create a bitmap object, as a new canvas object parameter, that is, the canvas is not blank, if you want to save the picture, it is best to bitmap is a new, rather than from a file to read in, or a Drawable object. Then use Canvas to draw the first picture, on the second picture, and finally use Canvas.save (int flag) method to save, note that the Save method inside the parameters can save a single layer, if it is to save all layers, use Save (Canvas.all_ Save_flag).

If you do not understand the picture rotation problem, please see the blog: Android learning note Bitmap bitmap rotation
This is the original picture and the post-rotation picture

This is the image saved to the local 800*600 jpg format

Specific implementation:

[Java]View PlainCopy
  1. Package xiaosi.bitmap;
  2. Import android.app.Activity;
  3. Import Android.os.Bundle;
  4. Public class Mianactivity extends Activity
  5. {
  6. private Bitmapview Bitmapview = null;
  7. @Override
  8. protected void OnCreate (Bundle savedinstancestate)
  9. {
  10. super.oncreate (savedinstancestate);
  11. Bitmapview = New Bitmapview (this);
  12. Setcontentview (Bitmapview);
  13. }
  14. }


Bitmapview.java

[Java]View PlainCopy
  1. Package xiaosi.bitmap;
  2. Import Java.io.File;
  3. Import Java.io.FileOutputStream;
  4. Import Android.content.Context;
  5. Import android.content.res.Resources;
  6. Import Android.graphics.Bitmap;
  7. Import Android.graphics.Bitmap.Config;
  8. Import Android.graphics.BitmapFactory;
  9. Import Android.graphics.Canvas;
  10. Import Android.graphics.Color;
  11. Import Android.graphics.Matrix;
  12. Import Android.view.View;
  13. Public class Bitmapview extends View
  14. {
  15. private Matrix Matrix = null;
  16. Public Bitmapview (context context)
  17. {
  18. super (context);
  19. }
  20. public void OnDraw (canvas canvas)
  21. {
  22. //Get reference to resource file res
  23. Resources res = getresources ();
  24. //Get graphics resource files
  25. Bitmap BMP = Bitmapfactory.decoderesource (res, r.drawable.h);
  26. //Set canvas canvas background to white
  27. Canvas.drawcolor (Color.Black);
  28. Canvas.drawbitmap (BMP, 0, 0, null);
  29. //define matrix object
  30. Matrix = new Matrix ();
  31. //Rotate 30 degrees
  32. Matrix.postrotate (30);
  33. Bitmap Bitmap = Bitmap.createbitmap (BMP, 0, bmp.getwidth (), bmp.getheight ()/2,
  34. Matrix, true);
  35. Canvas.drawbitmap (Bitmap, 0, + , null);
  36. Savebitmap (bitmap);
  37. }
  38. //Save to local
  39. public void Savebitmap (Bitmap bmp)
  40. {
  41. Bitmap Bitmap = Bitmap.createbitmap (+, config.argb_8888);
  42. Canvas canvas = new canvas (bitmap);
  43. //Load background image
  44. Bitmap BMPs = Bitmapfactory.decoderesource (Getresources (), r.drawable.playerbackground);
  45. Canvas.drawbitmap (BMPs, 0, 0, null);
  46. //load the screen to be saved
  47. Canvas.drawbitmap (BMP, ten, + , null);
  48. //Save All Layers
  49. Canvas.save (Canvas.all_save_flag);
  50. Canvas.restore ();
  51. //Storage path
  52. File File = new file ("/sdcard/song/");
  53. if (!file.exists ())
  54. File.mkdirs ();
  55. try {
  56. FileOutputStream FileOutputStream = new FileOutputStream (File.getpath () + "/xuanzhuan.jpg");
  57. Bitmap.compress (Bitmap.CompressFormat.JPEG, fileoutputstream);
  58. Fileoutputstream.close ();
  59. System.out.println ("savebmp is Here");
  60. } catch (Exception e) {
  61. E.printstacktrace ();
  62. }
  63. }
  64. }

Source code Download: Click to open the link

Android Learning notes Advanced 18 drawing and saving pictures to local

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.