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

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

<!-- 在SDCard中创建与删除文件权限 -->

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

<!-- 往SDCard写入数据权限 -->4<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

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

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.demo.picturetest"

    android:versionCode="1"

    android:versionName="1.0" >

    <!-- 在SDCard中创建与删除文件权限 -->

    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />

    <!-- 往SDCard写入数据权限 -->

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="19" />

    <application

        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name=".MainActivity"

            android:label="@string/app_name" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

</manifest>


2.详细代码如下

package com.demo.picturetest;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import android.app.Activity;

import android.graphics.Bitmap;

import android.graphics.Bitmap.Config;

import android.graphics.BitmapFactory;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.os.Bundle;

import android.os.Environment;

import android.util.Log;

import android.widget.ImageView;

public class MainActivity extends Activity {

    private Bitmap mBitmap;

    // 画布

    private Canvas mCanvas;

    // 画笔--画图片

    private Paint mPicturePaint = new Paint();

    // 画笔--写字

    private Paint mTextPaint = new Paint();

    // 画笔--写运动数据

    private Paint mDataPaint = new Paint();

    private ImageView mImageView;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        drawBitmap();

        init();

        // 展示

        mImageView.setImageBitmap(mBitmap);

    }

    private void init() {

        mImageView = (ImageView) findViewById(R.id.mImageView);

    }

    /**

    * 根据图片和文字生成图片

    * */

    private void drawBitmap() {

        Bitmap bm_bg = BitmapFactory.decodeResource(getResources(),

                R.drawable.dashboard_bg);

        // 得到图片的宽、高

        int width_bg = bm_bg.getWidth();

        int height_bg = bm_bg.getHeight();

        // 创建一个你需要尺寸的Bitmap

        mBitmap = Bitmap.createBitmap(width_bg, height_bg, Config.ARGB_8888);

        // 用这个Bitmap生成一个Canvas,然后canvas就会把内容绘制到上面这个bitmap中

        mCanvas = new Canvas(mBitmap);

        // 绘制背景图片

        mCanvas.drawBitmap(bm_bg, 0.0f, 0.0f, mPicturePaint);

        // 绘制图片

        Bitmap bm_head = BitmapFactory.decodeResource(getResources(),

                R.drawable.share_head_img);

        // 得到图片的宽、高

        int width_head = bm_head.getWidth();

        int height_head = bm_head.getHeight();

        // 绘制图片--保证其在水平方向居中

        mCanvas.drawBitmap(bm_head, (width_bg - width_head) / 2, 0.0f,

                mPicturePaint);

        // 绘制文字

        mTextPaint.setColor(Color.WHITE);// 白色画笔

        mTextPaint.setTextSize(80.0f);// 设置字体大小

        // 绘制文字

        mDataPaint.setColor(Color.RED);// 红色画笔

        mDataPaint.setTextSize(120.0f);// 设置字体大小

        String distanceTextString = "运动距离:";

        String distanceDataString = String.valueOf(888);

        String distanceScalString = "米";

        float distanceTextString_width = mTextPaint.measureText(

                distanceTextString, 0, distanceTextString.length());

        float distanceDataString_width = mDataPaint.measureText(

                distanceDataString, 0, distanceDataString.length());

        float distanceScalString_width = mTextPaint.measureText(

                distanceScalString, 0, distanceScalString.length());

        float x = (width_bg - distanceTextString_width

                - distanceDataString_width - distanceScalString_width) / 2;

        mCanvas.drawText(distanceTextString, x, height_head, mTextPaint);// 绘制文字

        mCanvas.drawText(distanceDataString, x + distanceTextString_width,

                height_head, mDataPaint);// 绘制文字

        mCanvas.drawText(distanceScalString, x + distanceTextString_width

                + distanceDataString_width, height_head, mTextPaint);// 绘制文字

        // 保存绘图为本地图片

        mCanvas.save(Canvas.ALL_SAVE_FLAG);

        mCanvas.restore();

        File file = new File(Environment.getExternalStorageDirectory()

                .getPath() + "/share_pic.png");// 保存到sdcard根目录下,文件名为share_pic.png

        Log.i("CXC", Environment.getExternalStorageDirectory().getPath());

        FileOutputStream fos = null;

        try {

            fos = new FileOutputStream(file);

            mBitmap.compress(Bitmap.CompressFormat.PNG, 50, fos);

        } catch (FileNotFoundException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

        try {

            fos.close();

        } catch (IOException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

    }

}

推荐阅读更多精彩内容