效果如下图所示

保存在sd卡上的文件为

Android 如何将Canvas上绘制的内容保存成本地图片(转)

手机上显示效果为:

Android 如何将Canvas上绘制的内容保存成本地图片(转)

1》》在Manifest文件中增加相应权限

 <!-- 在SDCard中创建与删除文件权限 --><br/>
 <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /><br/>
 <!-- 往SDCard写入数据权限 --><br/>
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

例如我的Manifest文件代码如下:

 <?xml version="1.0" encoding="utf-8"?><br/>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"<br/>
     package="com.demo.picturetest"<br/>
     android:versionCode="1"<br/>
     android:versionName="1.0" >  

     <!-- 在SDCard中创建与删除文件权限 --><br/>
     <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /><br/>
     <!-- 往SDCard写入数据权限 --><br/>
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  

     <uses-sdk<br/>
         android:minSdkVersion="8"<br/>
         android:targetSdkVersion="19" />  

     <application<br/>
         android:allowBackup="true"<br/>
         android:icon="@drawable/ic_launcher"<br/>
         android:label="@string/app_name"<br/>
         android:theme="@style/AppTheme" ><br/>
         <activity<br/>
             android:name=".MainActivity"<br/>
             android:label="@string/app_name" ><br/>
             <intent-filter><br/>
                 <action android:name="android.intent.action.MAIN" />  

                 <category android:name="android.intent.category.LAUNCHER" /><br/>
             </intent-filter><br/>
         </activity><br/>
     </application>  

 </manifest>  

2》》详细代码如下

 package com.demo.picturetest;  

 import java.io.File;<br/>
 import java.io.FileNotFoundException;<br/>
 import java.io.FileOutputStream;<br/>
 import java.io.IOException;  

 import android.app.Activity;<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.Paint;<br/>
 import android.os.Bundle;<br/>
 import android.os.Environment;<br/>
 import android.util.Log;<br/>
 import android.widget.ImageView;  

 public class MainActivity extends Activity {<br/>
     private Bitmap mBitmap;<br/>
     // 画布<br/>
     private Canvas mCanvas;<br/>
     // 画笔--画图片<br/>
     private Paint mPicturePaint = new Paint();<br/>
     // 画笔--写字<br/>
     private Paint mTextPaint = new Paint();<br/>
     // 画笔--写运动数据<br/>
     private Paint mDataPaint = new Paint();  

     private ImageView mImageView;  

     @Override<br/>
     protected void onCreate(Bundle savedInstanceState) {<br/>
         super.onCreate(savedInstanceState);<br/>
         setContentView(R.layout.activity_main);<br/>
         drawBitmap();<br/>
         init();<br/>
         // 展示<br/>
         mImageView.setImageBitmap(mBitmap);  

     }  

     private void init() {<br/>
         mImageView = (ImageView) findViewById(R.id.mImageView);<br/>
     }  

     /**<br/>
      * 根据图片和文字生成图片<br/>
      * */<br/>
     private void drawBitmap() {  

         Bitmap bm_bg = BitmapFactory.decodeResource(getResources(),<br/>
                 R.drawable.dashboard_bg);<br/>
         // 得到图片的宽、高<br/>
         int width_bg = bm_bg.getWidth();<br/>
         int height_bg = bm_bg.getHeight();  

         // 创建一个你需要尺寸的Bitmap<br/>
         mBitmap = Bitmap.createBitmap(width_bg, height_bg, Config.ARGB_8888);<br/>
         // 用这个Bitmap生成一个Canvas,然后canvas就会把内容绘制到上面这个bitmap中<br/>
         mCanvas = new Canvas(mBitmap);  

         // 绘制背景图片<br/>
         mCanvas.drawBitmap(bm_bg, 0.0f, 0.0f, mPicturePaint);<br/>
         // 绘制图片<br/>
         Bitmap bm_head = BitmapFactory.decodeResource(getResources(),<br/>
                 R.drawable.share_head_img);  

         // 得到图片的宽、高<br/>
         int width_head = bm_head.getWidth();<br/>
         int height_head = bm_head.getHeight();<br/>
         // 绘制图片--保证其在水平方向居中<br/>
         mCanvas.drawBitmap(bm_head, (width_bg - width_head) / 2, 0.0f,<br/>
                 mPicturePaint);  

         // 绘制文字<br/>
         mTextPaint.setColor(Color.WHITE);// 白色画笔<br/>
         mTextPaint.setTextSize(80.0f);// 设置字体大小  

         // 绘制文字<br/>
         mDataPaint.setColor(Color.RED);// 红色画笔<br/>
         mDataPaint.setTextSize(120.0f);// 设置字体大小  

         String distanceTextString = "运动距离:";<br/>
         String distanceDataString = String.valueOf(888);<br/>
         String distanceScalString = "米";  

         float distanceTextString_width = mTextPaint.measureText(<br/>
                 distanceTextString, 0, distanceTextString.length());  

         float distanceDataString_width = mDataPaint.measureText(<br/>
                 distanceDataString, 0, distanceDataString.length());<br/>
         float distanceScalString_width = mTextPaint.measureText(<br/>
                 distanceScalString, 0, distanceScalString.length());<br/>
         float x = (width_bg - distanceTextString_width<br/>
                 - distanceDataString_width - distanceScalString_width) / 2;  

         mCanvas.drawText(distanceTextString, x, height_head, mTextPaint);// 绘制文字<br/>
         mCanvas.drawText(distanceDataString, x + distanceTextString_width,<br/>
                 height_head, mDataPaint);// 绘制文字  

         mCanvas.drawText(distanceScalString, x + distanceTextString_width<br/>
                 + distanceDataString_width, height_head, mTextPaint);// 绘制文字  

         // 保存绘图为本地图片<br/>
         mCanvas.save(Canvas.ALL_SAVE_FLAG);<br/>
         mCanvas.restore();  

         File file = new File(Environment.getExternalStorageDirectory()<br/>
                 .getPath() + "/share_pic.png");// 保存到sdcard根目录下,文件名为share_pic.png<br/>
         Log.i("CXC", Environment.getExternalStorageDirectory().getPath());<br/>
         FileOutputStream fos = null;<br/>
         try {<br/>
             fos = new FileOutputStream(file);<br/>
             mBitmap.compress(Bitmap.CompressFormat.PNG, 50, fos);  

         } catch (FileNotFoundException e) {<br/>
             // TODO Auto-generated catch block<br/>
             e.printStackTrace();<br/>
         }<br/>
         try {<br/>
             fos.close();<br/>
         } catch (IOException e) {<br/>
             // TODO Auto-generated catch block<br/>
             e.printStackTrace();<br/>
         }  

     }<br/>
 }