java canvas保存_使用Android Paint类进行画图,怎么保存当前Canvas已画好的状态呢?...

好奇怪的,我想做一个简单的小画板,在Canvas上用手指绘制,现在想动态改变笔刷的颜色,粗细等属性,可是改变之后再次画的时候,原来已经画在Canvas上的线条也一起变为了新的设置了,请问如何解决这一问题呢?怎么样能让已经画好的不被Paint 新设置的颜色改变覆盖??望高手指点

,谢谢啦

package jet.dsm.demo;

import java.io.File;

import android.app.Activity;

import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.graphics.Canvas;

import android.graphics.Paint;

import android.graphics.Path;

import android.graphics.Rect;

import android.os.Environment;

import android.util.DisplayMetrics;

import android.view.MotionEvent;

import android.view.View;

/**

* 画笔类

*

* @author Yue

*

*/

public class Pen extends View {

private float mX, mY;

private Path mPath;

private Paint mPaint;

public Paint getmPaint() {

return mPaint;

}

public void setmPaint(Paint mPaint) {

this.mPaint = mPaint;

}

private static final float TOUCH_TOLERANCE = 4;

private Bitmap mBitmap;

private Canvas mCanvas;

private Paint mBitmapPaint;

private int screenWidth = 100;

private int screenHeight = 100;

public int getScreenWidth() {

return screenWidth;

}

public void setScreenWidth(int screenWidth) {

this.screenWidth = screenWidth;

}

public int getScreenHeight() {

return screenHeight;

}

public void setScreenHeight(int screenHeight) {

this.screenHeight = screenHeight;

}

public Pen(Context context, String pathName, String fileName,

int screenWdith, int screenHeight) {

super(context);

// TODO Auto-generated constructor stub

mPaint = Brush.getBrush();

// mBitmap = Bitmap.createBitmap(320,

// 480,Bitmap.Config.ARGB_8888);//绘制固定大小的bitmap对象

// mBitmap = this.getPhoto(fileName);

mBitmap = Pub.getPicture(pathName, fileName);

// String

// path=Environment.getExternalStorageDirectory().getPath()+"/"+fileName;

// mCanvas = new Canvas(BitmapFactory

// .decodeFile(pathName + "/" + fileName).copy(

// Bitmap.Config.ARGB_8888, true));

mCanvas = new Canvas();

// Rect src=new Rect(500, 500, mBitmap.getWidth(), mBitmap.getHeight());

// Rect dst=new Rect(0,0,100,500);

// mCanvas.drawBitmap(mBitmap, src, dst, null);

// mCanvas = new Canvas(Bitmap.createBitmap(mBitmap));//

// 将固定的bitmap对象嵌入到canvas对象中

mPath = new Path();// 创建画笔路径

mBitmapPaint = new Paint(Paint.DITHER_FLAG);

}

private void onTouchDown(float x, float y) {

// mPath.reset();// 将上次的路径保存起来,并重置新的路径。

mPath.moveTo(x, y);// 设置新的路径“轮廓”的开始

mX = x;

mY = y;

}

private void onTouchMove(float x, float y) {

float dx = Math.abs(x - mX);

float dy = Math.abs(y - mY);

if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {

mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);

mX = x;

mY = y;

}

}

private void onTouchUp(float x, float y) {

mPath.lineTo(mX, mY);// 从最后一个指定的xy点绘制一条线,如果没有用moveTo方法,那么起始点表示(0,0)点。

// commit the path to our offscreen

mCanvas.drawPath(mPath, mPaint);// 手指离开屏幕后,绘制创建的“所有”路径。

// kill this so we don't double draw

// mPath.reset();

}

@Override

protected void onDraw(Canvas canvas) {

// TODO Auto-generated method stub

canvas.drawColor(0xFFAAAAAA);

Rect src = new Rect(0, 0, mBitmap.getWidth(), mBitmap.getHeight());

Rect dst = new Rect(0, 0, this.screenWidth, this.screenHeight);

canvas.drawBitmap(mBitmap, src, dst, mBitmapPaint);

canvas.drawPath(mPath, mPaint);

super.onDraw(canvas);

}

@Override

public boolean onTouchEvent(MotionEvent event) {

// TODO Auto-generated method stub

float x = event.getX();

float y = event.getY();

switch (event.getAction()) {

case MotionEvent.ACTION_DOWN:// 手指开始按压屏幕,这个动作包含了初始化位置

onTouchDown(x, y);

invalidate();// 刷新画布,重新运行onDraw()方法

break;

case MotionEvent.ACTION_MOVE:// 手指按压屏幕时,位置的改变触发,这个方法在ACTION_DOWN和ACTION_UP之间。

onTouchMove(x, y);

invalidate();

break;

case MotionEvent.ACTION_UP:// 手指离开屏幕,不再按压屏幕

onTouchUp(x, y);

invalidate();

break;

default:

break;

}

return true;

}

/**

* 根据路径得到图片

*

* @param fileName

* @return

*/

public Bitmap getPhoto(String fileName) {

// SDCard的路径,也就是“/sdcard/”

File root = Environment.getExternalStorageDirectory();

// 图片

File wall = new File(root, fileName);

// 图片路径

String path = wall.getAbsolutePath();

// Log.d(TAG, "MM文件路径为:" + path);

return BitmapFactory.decodeFile(path);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值