0
アンドゥとリドゥ機能は以下のコードでは動作しません。以下は私の描画コードの実装です。アンドロイドキャンバスでアンドゥ/リドゥが動作しません
以下のコードに問題はありますか?
public class DrawingArea extends View { private Path drawPath; private Paint drawPaint, canvasPaint; private int paintColor = 0xFF660000; private Canvas drawCanvas; private Bitmap canvasBitmap; private ArrayList paths = new ArrayList(); private ArrayList undonePaths = new ArrayList(); private float mX, mY; private static final float TOUCH_TOLERANCE = 4; // for Undo, Redo private int historyPointer = 0; private boolean erase = false; public DrawingArea(Context context) { super(context); setupDrawing(); } public DrawingArea(Context context, AttributeSet attrs) { super(context, attrs); setupDrawing(); } public void setupDrawing() { drawPath = new Path(); drawPaint = new Paint(); drawPaint.setAntiAlias(true); drawPaint.setStrokeWidth(20); drawPaint.setStyle(Paint.Style.STROKE); drawPaint.setStrokeJoin(Paint.Join.ROUND); drawPaint.setStrokeCap(Paint.Cap.ROUND); canvasPaint = new Paint(Paint.DITHER_FLAG); //paths.add(drawPath); } public void setColor(String newColor) { invalidate(); paintColor = Color.parseColor(newColor); drawPaint.setColor(paintColor); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint); canvas.drawPath(drawPath, drawPaint); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); drawCanvas = new Canvas(canvasBitmap); } @Override public boolean onTouchEvent(MotionEvent event) { float x = event.getX(); float y = event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: 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; } private void touch_start(float x, float y) { drawPath.reset(); drawPath.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(y - mY); if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) { drawPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2); mX = x; mY = y; } } private void touch_up() { drawPath.lineTo(mX, mY); // commit the path to our offscreen drawCanvas.drawPath(drawPath, drawPaint); // kill this so we don't double draw drawPath = new Path(); paths.add(drawPath); } public void setErase() { canvasBitmap.eraseColor(Color.TRANSPARENT); drawPath.reset(); invalidate(); } public void startNew() { drawCanvas.drawColor(0, PorterDuff.Mode.CLEAR); invalidate(); } public void undo() { if (paths.size() > 0) { undonePaths.add(paths.remove(paths.size() - 1)); invalidate(); } } public void redo() { if (undonePaths.size() > 0) { paths.add(undonePaths.remove(undonePaths.size() - 1)); invalidate(); } } }
ではなく、ビットマップを描画せずに保持するのタイプを追加することを忘れないでください。パスがビットマップに描画されているので、元に戻すビットマップには影響しません。 – Pnemonic