0

I am able to erase image by converting it in to bitmap and setting it in canvas using following code. But i am not able to set undo and redo functionality. Following code change the source bitmap so how i save path and perform undo and redo functionality?

public class MyCustomView extends View
    {
        private Bitmap sourceBitmap;
        ImageButton undo, redo;
        private Canvas sourceCanvas = new Canvas();
        private Paint destPaint = new Paint();
        private Path destPath = new Path();

        Boolean IsEraserSet = false;

        public MyCustomView(Context context, Bitmap rawBitmap)
        {
            super(context);

            //converting drawable resource file into bitmap
           // Bitmap rawBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.attire);

            //converting bitmap into mutable bitmap
            this.undo = undo;
            this.redo = redo;
            sourceBitmap = Bitmap.createBitmap(rawBitmap.getWidth(), rawBitmap.getHeight(), Bitmap.Config.ARGB_8888);

            sourceCanvas.setBitmap(sourceBitmap);
            sourceCanvas.drawBitmap(rawBitmap, 0, 0, null);

            destPaint.setAlpha(0);
            destPaint.setAntiAlias(true);
            destPaint.setStyle(Paint.Style.STROKE);
            destPaint.setStrokeJoin(Paint.Join.ROUND);
            destPaint.setStrokeCap(Paint.Cap.ROUND);
            //change this value as per your need
            destPaint.setStrokeWidth(50);
            destPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));


        }




        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);

                 sourceCanvas.drawPath(destPath, destPaint);
                 canvas.drawBitmap(sourceBitmap, 0, 0, null);

           // sourceCanvas.drawPath(destPath, destPaint);
           // canvas.drawBitmap(sourceBitmap, 0, 0, null);

        }

        public void setEraser(Boolean value){

            IsEraserSet = value;
        }



        @Override
        public boolean onTouchEvent(MotionEvent event)
        {

            if(!IsEraserSet){

               return true;
            }
            float xPos = event.getX();
            float yPos = event.getY();

            switch (event.getAction())
            {
                case MotionEvent.ACTION_DOWN:

                    destPath.moveTo(xPos, yPos);

                    break;

                case MotionEvent.ACTION_MOVE:
                    destPath.lineTo(xPos, yPos);
                    break;

                case MotionEvent.ACTION_UP:
                    upTouch();

                    break;
            }

            invalidate();
            return true;
        }
    }

After erasing image its look like as below.I have added a background image below erased image. So actually i have to erase my top image to show the background image.But how can i add undo and redo functionality? Any help will always appreciated. A

Abhishek Joshi
  • 361
  • 1
  • 7
  • 17

1 Answers1

0

You should have a java.util.LinkedList<Path> instead of a Path. Iterate that list in onDraw and draw those paths to the canvas. When user clicks undo you just remove the last Path from the list and when user clicks redo, you add the recently removed Paths, that will be in a backup java.util.Stack<Path>.

GabrielBB
  • 2,479
  • 1
  • 35
  • 49