Android学习笔记进阶18之画图并保存图片到本地

1、首先创建一个Bitmap图片,并指定大小;
 
2、在该图片上创建一个新的画布Canvas,然后在画布上绘制,并保存即可;
 
3、需要保存的目录File,注意如果写的目录如“/sdcard/song/”如果不存在的话,要先创建(file.mkdirs).。


4、需要添加权限:<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Canvas是一个画布,你可以建立一个空白的画布,就直接new一个Canvas对象,不需要参数。也可以先使用BitmapFactory创建一个Bitmap对象,作为新的Canvas对象的参数,也就是说这个画布不是空白的,如果你想保存图片的话,最好是Bitmap是一个新的,而不是从某个文件中读入进来的,或者是Drawable对象。然后使用Canvas画第一张图上去,在画第二张图上去,最后使用Canvas.save(int flag)的方法进行保存,注意save方法里面的参数可以保存单个图层,如果是保存全部图层的 话使用 save( Canvas.ALL_SAVE_FLAG )。

 

关于图片旋转问题不懂的话,请看博文:Android学习笔记之Bitmap位图的旋转
                                这是原图片和旋转后图片

 

                                                                     这是保存到本地的图片800*600 JPG格式

 

具体实现:

package xiaosi.bitmap;

import android.app.Activity;
import android.os.Bundle;

public class mianActivity extends Activity
{

	private BitmapView bitmapView = null;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		bitmapView = new BitmapView(this);
		setContentView(bitmapView);
	}
}


BitmapView.java

package xiaosi.bitmap;

import java.io.File;
import java.io.FileOutputStream;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.view.View;

public class BitmapView extends View
{
	private Matrix matrix = null;
	public BitmapView(Context context)
	{
		super(context);
	}

	public void onDraw(Canvas canvas)
	{
		
		// 获取资源文件的引用res
		Resources res = getResources(); 
		// 获取图形资源文件
		Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.h);
		// 设置canvas画布背景为白色
		canvas.drawColor(Color.BLACK);
		canvas.drawBitmap(bmp, 0, 0, null);
		// 定义矩阵对象
		matrix = new Matrix();
		//旋转30度
		matrix.postRotate(30);
		Bitmap bitmap = Bitmap.createBitmap(bmp, 0, 50, bmp.getWidth(), bmp.getHeight()/2,
				matrix, true);
		canvas.drawBitmap(bitmap, 0, 250, null);
		SaveBitmap(bitmap);
	}
	//保存到本地
	public void SaveBitmap(Bitmap bmp)
	{
		Bitmap bitmap = Bitmap.createBitmap(800, 600, Config.ARGB_8888);  
		Canvas canvas = new Canvas(bitmap);
		//加载背景图片
		Bitmap bmps = BitmapFactory.decodeResource(getResources(), R.drawable.playerbackground);
		canvas.drawBitmap(bmps, 0, 0, null);
		//加载要保存的画面
		canvas.drawBitmap(bmp, 10, 100, null);
		//保存全部图层
		canvas.save(Canvas.ALL_SAVE_FLAG);
		canvas.restore();
		//存储路径
		File file = new File("/sdcard/song/");
		if(!file.exists())
			file.mkdirs();
			try {
				FileOutputStream fileOutputStream = new FileOutputStream(file.getPath() + "/xuanzhuan.jpg");
				bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
				fileOutputStream.close();
				System.out.println("saveBmp is here");
			} catch (Exception e) {
						e.printStackTrace();
		}
	}
}

 

 

源代码下载:点击打开链接

 

 

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值