1. Create a bitmap image and specify the size;
2. Create a new canvas on the image, draw it on the canvas, and save it;
3. File directory to be saved. Note that if the written directory is "/sdcard/song/", you must first create (file. mkdirs )..
4. Add permissions: <uses-Permission Android: Name = "android. Permission. write_external_storage"/>
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.
Use save (canvas. all_save_flag ).
For more information about image rotation, see blog: Bitmap bitmap rotation in Android learning notes
This is the original image and the rotated image.
This is the 800*600 JPG format of the image saved to the local device.
Specific implementation:
Package xiaosi. bitmap; </P> <p> Import android. app. activity; <br/> Import android. OS. bundle; </P> <p> public class mianactivity extends activity <br/>{</P> <p> private bitmapview = NULL; <br/> @ override <br/> protected void oncreate (bundle savedinstancestate) <br/>{< br/> super. oncreate (savedinstancestate); <br/> bitmapview = new bitmapview (this); <br/> setcontentview (bitmapview); <br/>}< br/>
Bitmapview. Java
Package xiaosi. bitmap; </P> <p> Import Java. io. file; <br/> Import Java. io. fileoutputstream; </P> <p> Import android. content. context; <br/> Import android. content. res. resources; <br/> Import android. graphics. bitmap; <br/> Import android. graphics. bitmap. config; <br/> Import android. graphics. bitmapfactory; <br/> Import android. graphics. canvas; <br/> Import android. graphics. color; <br/> Import android. graphics. matri X; <br/> Import android. view. view; </P> <p> public class bitmapview extends view <br/> {<br/> private matrix = NULL; <br/> Public bitmapview (context) <br/>{< br/> super (context); <br/>}</P> <p> Public void ondraw (canvas) <br/>{</P> <p> // obtain the resource file reference res <br/> resources res = getresources (); <br/> // obtain the image resource file <br/> bitmap BMP = bitmapfactory. decoderesource (Res, R. drawable. h); <br/> // set the canvas Canvas background is white <br/> canvas. drawcolor (color. black); <br/> canvas. drawbitmap (BMP, 0, 0, null); <br/> // defines a matrix object <br/> matrix = new matrix (); <br/> // rotate for 30 degrees <br/> matrix. postrotate (30); <br/> Bitmap bitmap = bitmap. createbitmap (BMP, 0, 50, BMP. getwidth (), BMP. getheight ()/2, <br/> matrix, true); <br/> canvas. drawbitmap (bitmap, 0,250, null); <br/> savebitmap (Bitmap ); <br/>}< br/> // save to a local device <br/> Public void savebitmap (bitm Ap bmp) <br/>{< br/> Bitmap bitmap = bitmap. createbitmap (800,600, config. argb_8888); <br/> canvas = new canvas (Bitmap); <br/> // load the background image <br/> bitmap BMP = bitmapfactory. decoderesource (getresources (), R. drawable. playerbackground); <br/> canvas. drawbitmap (bmp s, 0, 0, null); <br/> // load the image to be saved <br/> canvas. drawbitmap (BMP, 10,100, null); <br/> // save all layers <br/> canvas. save (canvas. all_save_flag); <br/> canva S. Restore (); <br/> // storage path <br/> file = new file ("/sdcard/song/"); <br/> If (! File. exists () <br/> file. mkdirs (); <br/> try {<br/> fileoutputstream = new fileoutputstream (file. getpath () + "/xuanzhuan.jpg"); <br/> bitmap. compress (bitmap. compressformat. JPEG, 100, fileoutputstream); <br/> fileoutputstream. close (); <br/> system. out. println ("savebmp is here"); <br/>} catch (exception e) {<br/> E. printstacktrace (); <br/>}< br/>
Source codeDownload: Click the open link