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
- Package xiaosi.bitmap;
- Import android.app.Activity;
- Import Android.os.Bundle;
- Public class Mianactivity extends Activity
- {
- private Bitmapview Bitmapview = null;
- @Override
- protected void OnCreate (Bundle savedinstancestate)
- {
- super.oncreate (savedinstancestate);
- Bitmapview = New Bitmapview (this);
- Setcontentview (Bitmapview);
- }
- }
Bitmapview.java
[Java]View PlainCopy
- Package xiaosi.bitmap;
- Import Java.io.File;
- Import Java.io.FileOutputStream;
- Import Android.content.Context;
- Import android.content.res.Resources;
- Import Android.graphics.Bitmap;
- Import Android.graphics.Bitmap.Config;
- Import Android.graphics.BitmapFactory;
- Import Android.graphics.Canvas;
- Import Android.graphics.Color;
- Import Android.graphics.Matrix;
- Import Android.view.View;
- Public class Bitmapview extends View
- {
- private Matrix Matrix = null;
- Public Bitmapview (context context)
- {
- super (context);
- }
- public void OnDraw (canvas canvas)
- {
- //Get reference to resource file res
- Resources res = getresources ();
- //Get graphics resource files
- Bitmap BMP = Bitmapfactory.decoderesource (res, r.drawable.h);
- //Set canvas canvas background to white
- Canvas.drawcolor (Color.Black);
- Canvas.drawbitmap (BMP, 0, 0, null);
- //define matrix object
- Matrix = new Matrix ();
- //Rotate 30 degrees
- Matrix.postrotate (30);
- Bitmap Bitmap = Bitmap.createbitmap (BMP, 0, bmp.getwidth (), bmp.getheight ()/2,
- Matrix, true);
- Canvas.drawbitmap (Bitmap, 0, + , null);
- Savebitmap (bitmap);
- }
- //Save to local
- public void Savebitmap (Bitmap bmp)
- {
- Bitmap Bitmap = Bitmap.createbitmap (+, config.argb_8888);
- Canvas canvas = new canvas (bitmap);
- //Load background image
- Bitmap BMPs = Bitmapfactory.decoderesource (Getresources (), r.drawable.playerbackground);
- Canvas.drawbitmap (BMPs, 0, 0, null);
- //load the screen to be saved
- Canvas.drawbitmap (BMP, ten, + , null);
- //Save All Layers
- Canvas.save (Canvas.all_save_flag);
- Canvas.restore ();
- //Storage path
- File File = new file ("/sdcard/song/");
- if (!file.exists ())
- File.mkdirs ();
- try {
- FileOutputStream FileOutputStream = new FileOutputStream (File.getpath () + "/xuanzhuan.jpg");
- Bitmap.compress (Bitmap.CompressFormat.JPEG, fileoutputstream);
- Fileoutputstream.close ();
- System.out.println ("savebmp is Here");
- } catch (Exception e) {
- E.printstacktrace ();
- }
- }
- }
Source code Download: Click to open the link
Android Learning notes Advanced 18 drawing and saving pictures to local