android图像绘制(五)——画布保存为图片
上一篇:http://www.zzzyk.com/kf/201202/119625.html
将图片进行编辑(放缩,涂鸦等),最后保存成指定格式、大小的图片。
先贴代码:
[java]
Bitmap bmp = Bitmap.createBitmap(480, 800, Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
canvas.drawBitmap(this.bmp, matrix, paint);
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
File file = new File("/sdcard/akai/");
if(!file.exists())
file.mkdirs();
try {
FileOutputStream fos = new FileOutputStream(file.getPath() + "/2.png");
bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
System.out.println("saveBmp is here");
} catch (Exception e) {
e.printStackTrace();
}
Bitmap bmp = Bitmap.createBitmap(480, 800, Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
canvas.drawBitmap(this.bmp, matrix, paint);
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
File file = new File("/sdcard/akai/");
if(!file.exists())
file.mkdirs();
try {
FileOutputStream fos = new FileOutputStream(file.getPath() + "/2.png");
bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
System.out.println("saveBmp is here");
} catch (Exception e) {
e.printStackTrace();
}解释:
1、首先创建一个Bitmap图片,并指定大小;
2、在该图片上创建一个新的画布Canvas,然后在画布上绘制,并保存即可;
3、需要保存的目录File,注意如果写的目录如“/sdcard/akai/”如果不存在的话,要先创建(file.mkdirs()),否则FileOutputStream会报错No found;
4、需要添加权限:<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
摘自 阿凯的专栏
补充:移动开发 , Android ,