How to erase image with undo and redo functionality in android?

199
February 15, 2018, at 11:29 PM

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.

Answer 1

Phimpme Android other than editing the image is the ability to revert the changes (UNDO) that are done and the ability to revert the reversion i.e reperform the changes (REDO).

refer this link: https://blog.fossasia.org/implementing-undo-and-redo-in-image-editor-of-phimpme-android/

READ ALSO
How to change/update the custom layout of a Dialog on orientation change in android

How to change/update the custom layout of a Dialog on orientation change in android

Im trying to change the layout of a Dialog in the onConfigurationChange of the activityI have created 2 layout one in regular layouts folder and other in layouts-land, i can get the layouts if the orientations of the device is in landscape or portrait...

108
Oppo mobile push notification issue when app not running [on hold]

Oppo mobile push notification issue when app not running [on hold]

Oppo mobile cant receive push notification when app is not runningBut Samsung , Lenovo mobile can receive push notification even if app not running

99
trouble with firebase on android studio

trouble with firebase on android studio

I currently programming a Blog App for androidBut I have problems with the firebase database

153
Print logo on POS printer [on hold]

Print logo on POS printer [on hold]

Just Wondering if someone had experience working with printing logo on POS printerI'm using some Chinese printer model Excelvan with Android SDK

118