AndroidStudioでお絵かきアプリを作っています。パレットから色を選択してcanvas上にお絵かきすることはできるのですが、undo機能をつけようとしたところ、パスがcanvas上から消えてくれません。
canvasを完全に理解できないまま作っていたので、調べて実装してみても中々上手くいかないため質問させていただきました。
PaintView.java
java
1public class PaintView extends View { 2 public static int BLUSH_SIZE = 12; 3 public static final int DEFAULT_COLOR = Color.BLACK; 4 private static final float TOUCH_TOLERANCE = 4; 5 6 public static int ALPHA = 110; 7 public static int RED = 0x99; 8 public static int GREEN = 0xCC; 9 public static int BLUE = 0xFF; 10 11 private float mX, mY; 12 13 private Path mPath; 14 private Paint mPaint; 15 private ArrayList<FingerPath> paths; 16 17 private int currentColor; 18 private int strokeWidth; 19 20 private Canvas mCanvas; 21 private Bitmap mBitmap; 22 private Paint mBitmapPoint = new Paint(Paint.DITHER_FLAG); 23 24 public PaintView(Context context) { super(context); } 25 26 public PaintView(Context context, @Nullable AttributeSet attrs) { 27 super(context, attrs); 28 29 mPaint = new Paint(); 30 mPaint.setAntiAlias(true); 31 mPaint.setDither(true); 32 mPaint.setColor(DEFAULT_COLOR); 33 mPaint.setStyle(Paint.Style.STROKE); 34 mPaint.setStrokeJoin(Paint.Join.ROUND); 35 mPaint.setStrokeCap(Paint.Cap.ROUND); 36 mPaint.setXfermode(null); 37 38 this.paths = new ArrayList<>(); 39 } 40 41 public void init(Bitmap bmp, int r, int g, int b) { 42 this.RED = r; 43 this.GREEN = g; 44 this.BLUE = b; 45 mBitmap = bmp; 46 mCanvas = new Canvas(mBitmap); 47 48 currentColor = DEFAULT_COLOR; 49 strokeWidth = BLUSH_SIZE; 50 51 mPaint.setShader(new LinearGradient(0,0,100,100,Color.argb(ALPHA,RED,GREEN,BLUE), 52 Color.argb(ALPHA,RED,GREEN,BLUE), Shader.TileMode.REPEAT)); 53 } 54 55 56 @Override 57 protected void onDraw(Canvas canvas) { 58 super.onDraw(canvas); 59 canvas.save(); 60 61 for (FingerPath fp: paths) { 62 mPaint.setColor(fp.color); 63 mPaint.setStrokeWidth(fp.strokeWidth); 64 mPaint.setMaskFilter(null); 65 mCanvas.drawPath(fp.mPath, mPaint); 66 } 67 68 mCanvas.drawPath(this.mPath, this.mPaint); 69 70 canvas.drawBitmap(mBitmap, 0, 0, mBitmapPoint); 71 canvas.restore(); 72 } 73 74 private void touchStart(float x, float y) { 75 76 mPath = new Path(); 77 78 FingerPath fp = new FingerPath(currentColor, strokeWidth, mPath); 79 paths.add(fp); 80 81 mPath.reset(); 82 mPath.moveTo(x, y); 83 mX = x; 84 mY = y; 85 } 86 87 private void touchMove(float x, float y) { 88 float dx = Math.abs(x - mX); 89 float dy = Math.abs(y - mY); 90 91 if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) { 92 mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2); 93 mX = x; 94 mY = y; 95 } 96 } 97 98 private void touchUp() { 99 mPath.lineTo(mX, mY); 100 } 101 102 @Override 103 public boolean onTouchEvent(MotionEvent event) { 104 float x = event.getX(); 105 float y = event.getY(); 106 107 switch (event.getAction()) { 108 case MotionEvent.ACTION_DOWN: 109 touchStart(x, y); 110 invalidate(); 111 break; 112 case MotionEvent.ACTION_MOVE: 113 touchMove(x, y); 114 invalidate(); 115 break; 116 case MotionEvent.ACTION_UP: 117 touchUp(); 118 invalidate(); 119 this.mPath.reset(); 120 break; 121 } 122 123 return true; 124 } 125 126 public void undo() { 127 if(!paths.isEmpty()) { 128 int size = paths.size(); 129 Log.d("TAG", "undoPrevPath1: "+size); 130 paths.remove(paths.size()-1); 131 Log.d("TAG", "undoPrevPath2: "+paths.size()); 132 } 133 invalidate(); 134 }
FingerPath.java
java
1public class FingerPath { 2 public int color; 3 public int strokeWidth; 4 public Path mPath; 5 6 public FingerPath(int color, int strokeWidth, Path path) { 7 this.color = color; 8 this.strokeWidth = strokeWidth; 9 mPath = path; 10 11 } 12}
このクラスは他のクラスから下記のようにして呼び出しています。
java
1switch (pos) { 2 case 0: 3 // black 4 mPaintView.init(paintBitmap, 0x16, 0x16, 0x16); 5 break; 6 case 1: 7 // deep_koamaru 8 mPaintView.init(paintBitmap, 35, 54, 104); 9 break; 10 case 2: 11 // pastel_blue 12 mPaintView.init(paintBitmap, 160, 195, 210); 13 break; 14 case 3: 15 // lavender_gray 16 mPaintView.init(paintBitmap, 190, 190, 209); 17 break; 18 case 4: 19 // queen_pink 20 mPaintView.init(paintBitmap, 248, 205, 210); 21 break; 22 case 5: 23 // orange_yellow 24 mPaintView.init(paintBitmap, 249, 200, 99); 25 break; 26 case 6: 27 // white 28 mPaintView.init(paintBitmap, 255, 255, 255); 29 break; 30 default: 31 return; 32 }
個人的にundoメソッド内でinvalidate()をした時に一つ前のパスを消した最新のcanvasが反映されていなくてこうなっているのかと思うのですが、教えていただきたいです。よろしくお願いいたします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。