Android簡單塗鴉以及撤銷、重做的實現方法
阿新 • • 發佈:2019-02-07
前段時間研究了下塗鴉功能的實現,其實單獨的塗鴉實現起來還是挺簡單的,關鍵的技術難點是撤銷與重做功能的實現。但是這裡暫時只說明下塗鴉功能的實現,高手勿噴哈,而且該功能在Android SDK提供的APIDemo當中就有的,但是如果能夠將該地方的知識點搞懂的話,我認為View畫圖基本上是難不倒你了,特別是裡面為什麼要用一箇中間的Bitmap。老規矩,還是先看看效果圖吧:
程式碼如下:
- package cn.ych.tuya;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.Canvas;
- import android.graphics.Paint;
- import android.graphics.Path;
- import android.graphics.Bitmap.CompressFormat;
- import android.os.Environment;
- import android.view.MotionEvent;
- import android.view.View;
- /**
- *
- * @category: View實現塗鴉、撤銷以及重做功能
- * @author: 鋒翼
- * @link: www.apkstory.com
- * @date: 2012.1.4
- *
- */
- public class TuyaView extends View {
- private Bitmap mBitmap;
- private Canvas mCanvas;
- private Path mPath;
- private Paint mBitmapPaint;// 畫布的畫筆
- private Paint mPaint;// 真實的畫筆
- private float mX, mY;//臨時點座標
- private static final float TOUCH_TOLERANCE = 4;
- private int screenWidth, screenHeight;// 螢幕長寬
- public TuyaView(Context context, int w, int h) {
- super(context);
- screenWidth = w;
- screenHeight = h;
- mBitmap = Bitmap.createBitmap(screenWidth, screenHeight,
- Bitmap.Config.ARGB_8888);
- // 儲存一次一次繪製出來的圖形
- mCanvas = new Canvas(mBitmap);
- mBitmapPaint = new Paint(Paint.DITHER_FLAG);
- mPaint = new Paint();
- mPaint.setAntiAlias(true);
- mPaint.setStyle(Paint.Style.STROKE);
- mPaint.setStrokeJoin(Paint.Join.ROUND);// 設定外邊緣
- mPaint.setStrokeCap(Paint.Cap.SQUARE);// 形狀
- mPaint.setStrokeWidth(5);// 畫筆寬度
- }
- @Override
- public void onDraw(Canvas canvas) {
- canvas.drawColor(0xFFAAAAAA);
- // 將前面已經畫過得顯示出來
- canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
- if (mPath != null) {
- // 實時的顯示
- canvas.drawPath(mPath, mPaint);
- }
- }
- private void touch_start(float x, float y) {
- mPath.moveTo(x, y);
- mX = x;
- mY = y;
- }
- private void touch_move(float x, float y) {
- float dx = Math.abs(x - mX);
- float dy = Math.abs(mY - y);
- 觸控間隔大於閾值才繪製路徑
- if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
- // 從x1,y1到x2,y2畫一條貝塞爾曲線,更平滑(直接用mPath.lineTo也是可以的)
- mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
- mX = x;
- mY = y;
- }
- }
- private void touch_up() {
- mPath.lineTo(mX, mY);
- mCanvas.drawPath(mPath, mPaint);
- }
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- float x = event.getX();
- float y = event.getY();
- switch (event.getAction()) {
- case MotionEvent.ACTION_DOWN:
- // 每次down下去重新new一個Path
- mPath = new Path();
- touch_start(x, y);
- invalidate();
- break;
- case MotionEvent.ACTION_MOVE:
- touch_move(x, y);
- invalidate();
- break;
- case MotionEvent.ACTION_UP:
- touch_up();
- invalidate();
- break;
- }
- return true;
- }
- }
上一講當中,已經講解了普通View實現塗鴉的功能,現在再來給塗鴉新增上撤銷與重做的功能吧。撤銷與重做在很多地方都是很重要的功能,比如PS裡面、Word裡面等等,而且大部分童鞋都能夠想到要實現該功能應該需要用到堆疊,對於一些大牛的話可能就直接想到設計模式上面去了,比如命令模式就可以解決撤銷與重做的問題。我們這裡要講解的是利用集合來完成該功能,其實也就是模擬棧,我相信你懂得。
老規矩,先上效果圖:
程式碼如下:
- package cn.ych.tuya;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.Canvas;
- import android.graphics.Paint;
- import android.graphics.Path;
- import android.graphics.Bitmap.CompressFormat;
- import android.os.Environment;
- import android.view.MotionEvent;
- import android.view.View;
- /**
- *
- * @category: View實現塗鴉、撤銷以及重做功能
- * @author: 鋒翼
- * @link: www.apkstory.com
- * @date: 2012.1.4
- *
- */
- public class TuyaView extends View {
- private Bitmap mBitmap;
- private Canvas mCanvas;
- private Path mPath;
- private Paint mBitmapPaint;// 畫布的畫筆
- private Paint mPaint;// 真實的畫筆
- private float mX, mY;// 臨時點座標
- private static final float TOUCH_TOLERANCE = 4;
- // 儲存Path路徑的集合,用List集合來模擬棧
- private static List<DrawPath> savePath;
- // 記錄Path路徑的物件
- private DrawPath dp;
- private int screenWidth, screenHeight;// 螢幕長寬
- private class DrawPath {
- public Path path;// 路徑
- public Paint paint;// 畫筆
- }
- public TuyaView(Context context, int w, int h) {
- super(context);
- screenWidth = w;
- screenHeight = h;
- mBitmap = Bitmap.createBitmap(screenWidth, screenHeight,
- Bitmap.Config.ARGB_8888);
- // 儲存一次一次繪製出來的圖形
- mCanvas = new Canvas(mBitmap);
- mBitmapPaint = new Paint(Paint.DITHER_FLAG);
- mPaint = new Paint();
- mPaint.setAntiAlias(true);
- mPaint.setStyle(Paint.Style.STROKE);
- mPaint.setStrokeJoin(Paint.Join.ROUND);// 設定外邊緣
- mPaint.setStrokeCap(Paint.Cap.SQUARE);// 形狀
- mPaint.setStrokeWidth(5);// 畫筆寬度
- savePath = new ArrayList<DrawPath>();
- }
- @Override
- public void onDraw(Canvas canvas) {
- canvas.drawColor(0xFFAAAAAA);
- // 將前面已經畫過得顯示出來
- canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
- if (mPath != null) {
- // 實時的顯示
- canvas.drawPath(mPath, mPaint);
- }
- }
- private void touch_start(float x, float y) {
- mPath.moveTo(x, y);
- mX = x;
- mY = y;
- }
- private void touch_move(float x, float y) {
- float dx = Math.abs(x - mX);
- float dy = Math.abs(mY - y);
- if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
- // 從x1,y1到x2,y2畫一條貝塞爾曲線,更平滑(直接用mPath.lineTo也是可以的)
- mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
- mX = x;
- mY = y;
- }
- }
- private void touch_up() {
- mPath.lineTo(mX, mY);
- mCanvas.drawPath(mPath, mPaint);
- //將一條完整的路徑儲存下來(相當於入棧操作)
- savePath.add(dp);
- mPath = null;// 重新置空
- }
- /**
- * 撤銷的核心思想就是將畫布清空,
- * 將儲存下來的Path路徑最後一個移除掉,
- * 重新將路徑畫在畫布上面。
- */
- public void undo() {
- mBitmap = Bitmap.createBitmap(screenWidth, screenHeight,
- Bitmap.Config.ARGB_8888);
- mCanvas.setBitmap(mBitmap);// 重新設定畫布,相當於清空畫布
- // 清空畫布,但是如果圖片有背景的話,則使用上面的重新初始化的方法,用該方法會將背景清空掉...
- if (savePath != null && savePath.size() > 0) {
- // 移除最後一個path,相當於出棧操作
- savePath.remove(savePath.size() - 1);
- Iterator<DrawPath> iter = savePath.iterator();
- while (iter.hasNext()) {
- DrawPath drawPath = iter.next();
- mCanvas.drawPath(drawPath.path, drawPath.paint);
- }
- invalidate();// 重新整理
- /*在這裡儲存圖片純粹是為了方便,儲存圖片進行驗證*/
- String fileUrl = Environment.getExternalStorageDirectory()
- .toString() + "/android/data/test.png";
- try {
- FileOutputStream fos = new FileOutputStream(new File(fileUrl));
- mBitmap.compress(CompressFormat.PNG, 100, fos);
- fos.flush();
- fos.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- /**
- * 重做的核心思想就是將撤銷的路徑儲存到另外一個集合裡面(棧),
- * 然後從redo的集合裡面取出最頂端物件,
- * 畫在畫布上面即可。
- */
- public void redo(){
- //如果撤銷你懂了的話,那就試試重做吧。
- }
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- float x = event.getX();
- float y = event.getY();
- switch (event.getAction()) {
- case MotionEvent.ACTION_DOWN:
- // 每次down下去重新new一個Path
- mPath = new Path();
- //每一次記錄的路徑物件是不一樣的
- dp = new DrawPath();
- dp.path = mPath;
- dp.paint = mPaint;
- touch_start(x, y);
- invalidate();
- break;
- case MotionEvent.ACTION_MOVE:
- touch_move(x, y);
- invalidate();
- break;
- case MotionEvent.ACTION_UP:
- touch_up();
- invalidate();
- break;
- }
- return true;
- }
- }