view plain copy to clipboard print ?
- Bitmap bm = Bitmap.createBitmap( 320 , 480 , Config.ARGB_8888);
- Canvas canvas = new Canvas(bm);
- Paint p = new Paint();
- canvas.drawRect(50 , 50 , 200 , 200 , p);
- canvas.save(Canvas.ALL_SAVE_FLAG );
- canvas.restore();
- File f = new File( "/sdcard/0.png" );
- FileOutputStream fos = null ;
- try {
- fos = new FileOutputStream(f);
- bm.compress(Bitmap.CompressFormat.PNG, 50 , fos);
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
這樣就把Canvas中的内容儲存成了sd卡上的一個png圖檔。簡單說一下原理,自己create一個bitmap,然後讓Canvas通過這個bitmap建立一個執行個體,然後在該canvas上繪制的内容都會畫 在該bitmap上,儲存Canvas的圖層,然後把bitmap寫到sd卡上的檔案。注意:要在AndroidManifest.xml中添加在sd卡上讀寫檔案的權限