Android 如何将Canvas上绘制的内容保存成本地图片(转)
效果如下图所示
保存在sd卡上的文件为
手机上显示效果为:
1》》在Manifest文件中增加相应权限
1 <!-- 在SDCard中创建与删除文件权限 --> 2 <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> 3 <!-- 往SDCard写入数据权限 --> 4 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
例如我的Manifest文件代码如下:
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.demo.picturetest" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 7 <!-- 在SDCard中创建与删除文件权限 --> 8 <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> 9 <!-- 往SDCard写入数据权限 --> 10 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 11 12 <uses-sdk 13 android:minSdkVersion="8" 14 android:targetSdkVersion="19" /> 15 16 <application 17 android:allowBackup="true" 18 android:icon="@drawable/ic_launcher" 19 android:label="@string/app_name" 20 android:theme="@style/AppTheme" > 21 <activity 22 android:name=".MainActivity" 23 android:label="@string/app_name" > 24 <intent-filter> 25 <action android:name="android.intent.action.MAIN" /> 26 27 <category android:name="android.intent.category.LAUNCHER" /> 28 </intent-filter> 29 </activity> 30 </application> 31 32 </manifest>
2》》详细代码如下
1 package com.demo.picturetest; 2 3 import java.io.File; 4 import java.io.FileNotFoundException; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 8 import android.app.Activity; 9 import android.graphics.Bitmap; 10 import android.graphics.Bitmap.Config; 11 import android.graphics.BitmapFactory; 12 import android.graphics.Canvas; 13 import android.graphics.Color; 14 import android.graphics.Paint; 15 import android.os.Bundle; 16 import android.os.Environment; 17 import android.util.Log; 18 import android.widget.ImageView; 19 20 public class MainActivity extends Activity { 21 private Bitmap mBitmap; 22 // 画布 23 private Canvas mCanvas; 24 // 画笔--画图片 25 private Paint mPicturePaint = new Paint(); 26 // 画笔--写字 27 private Paint mTextPaint = new Paint(); 28 // 画笔--写运动数据 29 private Paint mDataPaint = new Paint(); 30 31 private ImageView mImageView; 32 33 @Override 34 protected void onCreate(Bundle savedInstanceState) { 35 super.onCreate(savedInstanceState); 36 setContentView(R.layout.activity_main); 37 drawBitmap(); 38 init(); 39 // 展示 40 mImageView.setImageBitmap(mBitmap); 41 42 } 43 44 private void init() { 45 mImageView = (ImageView) findViewById(R.id.mImageView); 46 } 47 48 /** 49 * 根据图片和文字生成图片 50 * */ 51 private void drawBitmap() { 52 53 Bitmap bm_bg = BitmapFactory.decodeResource(getResources(), 54 R.drawable.dashboard_bg); 55 // 得到图片的宽、高 56 int width_bg = bm_bg.getWidth(); 57 int height_bg = bm_bg.getHeight(); 58 59 // 创建一个你需要尺寸的Bitmap 60 mBitmap = Bitmap.createBitmap(width_bg, height_bg, Config.ARGB_8888); 61 // 用这个Bitmap生成一个Canvas,然后canvas就会把内容绘制到上面这个bitmap中 62 mCanvas = new Canvas(mBitmap); 63 64 // 绘制背景图片 65 mCanvas.drawBitmap(bm_bg, 0.0f, 0.0f, mPicturePaint); 66 // 绘制图片 67 Bitmap bm_head = BitmapFactory.decodeResource(getResources(), 68 R.drawable.share_head_img); 69 70 // 得到图片的宽、高 71 int width_head = bm_head.getWidth(); 72 int height_head = bm_head.getHeight(); 73 // 绘制图片--保证其在水平方向居中 74 mCanvas.drawBitmap(bm_head, (width_bg - width_head) / 2, 0.0f, 75 mPicturePaint); 76 77 // 绘制文字 78 mTextPaint.setColor(Color.WHITE);// 白色画笔 79 mTextPaint.setTextSize(80.0f);// 设置字体大小 80 81 // 绘制文字 82 mDataPaint.setColor(Color.RED);// 红色画笔 83 mDataPaint.setTextSize(120.0f);// 设置字体大小 84 85 String distanceTextString = "运动距离:"; 86 String distanceDataString = String.valueOf(888); 87 String distanceScalString = "米"; 88 89 float distanceTextString_width = mTextPaint.measureText( 90 distanceTextString, 0, distanceTextString.length()); 91 92 float distanceDataString_width = mDataPaint.measureText( 93 distanceDataString, 0, distanceDataString.length()); 94 float distanceScalString_width = mTextPaint.measureText( 95 distanceScalString, 0, distanceScalString.length()); 96 float x = (width_bg - distanceTextString_width 97 - distanceDataString_width - distanceScalString_width) / 2; 98 99 mCanvas.drawText(distanceTextString, x, height_head, mTextPaint);// 绘制文字 100 mCanvas.drawText(distanceDataString, x + distanceTextString_width, 101 height_head, mDataPaint);// 绘制文字 102 103 mCanvas.drawText(distanceScalString, x + distanceTextString_width 104 + distanceDataString_width, height_head, mTextPaint);// 绘制文字 105 106 // 保存绘图为本地图片 107 mCanvas.save(Canvas.ALL_SAVE_FLAG); 108 mCanvas.restore(); 109 110 File file = new File(Environment.getExternalStorageDirectory() 111 .getPath() + "/share_pic.png");// 保存到sdcard根目录下,文件名为share_pic.png 112 Log.i("CXC", Environment.getExternalStorageDirectory().getPath()); 113 FileOutputStream fos = null; 114 try { 115 fos = new FileOutputStream(file); 116 mBitmap.compress(Bitmap.CompressFormat.PNG, 50, fos); 117 118 } catch (FileNotFoundException e) { 119 // TODO Auto-generated catch block 120 e.printStackTrace(); 121 } 122 try { 123 fos.close(); 124 } catch (IOException e) { 125 // TODO Auto-generated catch block 126 e.printStackTrace(); 127 } 128 129 } 130 }
转自:http://blog.csdn.net/cxc19890214/article/details/39924951
联系方式:emhhbmdfbGlhbmcxOTkxQDEyNi5jb20=