How to implemement undo features in painting appli

2019-02-25 20:48发布

问题:

i want to do undo features in in my application.. for this i searched i net found that take arraylist of x,y points which i have done below code i am unable for undo the drawings ??

public Drawview(Context context, float x, float y, int r) {
            super(context);


            mBitmap = Bitmap.createBitmap(800, 1200, Bitmap.Config.ARGB_8888);
           // mainbitmap.add(mBitmap);
            //System.out.println("**mBitmapPaint***"+mBitmapPaint);
            mCanvas = new Canvas(mBitmap);
            mPath = new Path();
            mBitmapPaint = new Paint(Paint.DITHER_FLAG);



        }

       @Override
       protected void onSizeChanged(int w, int h, int oldw, int oldh) {
           super.onSizeChanged(w, h, oldw, oldh);
       }

        @Override 
        protected void onDraw(Canvas canvas) {

            System.out.println("***Drawinglines****"+Drawinglines);

                canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
                canvas.drawPath(mPath, mPaint);



        }


        private float mX, mY;
        private static final float TOUCH_TOLERANCE = 4;

        private void touch_start(float x, float y) {
            mPath.reset();
            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(y - mY);
            if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
                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);
        ///    path.
            //graphics.add(mPath);
            mPath.reset();
        }

        // events when touching the screen
        public boolean onTouchEvent(MotionEvent event) {

             event.getAction();
            int eventaction = event.getAction(); 

            int X = (int)event.getX(); 
            int Y = (int)event.getY(); 

            switch (eventaction ) { 

            case MotionEvent.ACTION_DOWN: 

            {
                touch_start(X, Y);
                }
                 invalidate(); 
                 break; 


            case MotionEvent.ACTION_MOVE:  

                System.out.println("***ACTION_MOVE**************"+X+"************"+Y);
                if((Drawinglines.equals("Pen"))||(Drawinglines.equals("Erase")))
                {
                  touch_move(X, Y);
                  temp_point.add(new Point(X,Y));

                }
                invalidate(); 
                break; 

            case MotionEvent.ACTION_UP: 

                main_Points.add(temp_point);
                temp_point = null;

                 touch_up();  

                 invalidate(); 
                 break; 
            } 

            return true; 

        }




        public void undopath()
        {   
              main_Points.remove(main_Points.size()-1);
             invalidate();
        }

    }
    @Override
    public void onClick(View v) {


        if(v ==  Undo)
        {    
            Drawview abc=new Drawview();
             abc.undopath();
        }

But undo undo functionality is not working.

回答1:

private Slate mSlate;
private TiledBitmapCanvas mTiledCanvas;

 public void clickUndo(View unused) {

        mSlate.undo();
    }


public void undo() {
        if (mTiledCanvas == null) {
            Log.v(TAG, "undo before mTiledCanvas inited");
        }
        mTiledCanvas.step(-1);

        invalidate();
    }


回答2:

I didn't try, but i think use two canvas and bitmaps is helpful, on draw the current state, one is still former state, only need to restore the former one. But it only can restore on step.



回答3:

here some code: create:

private Canvas mCanvas;
private Bitmap mBitmap;

private Canvas mRestoreCanvas;
private Bitmap mRestoreBitmap;

init:

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    mPictureWidth = w;
    mPictureHeight = h;

    Loge.i("mPictureWidth = " + mPictureWidth + " mPictureHeight = "
            + mPictureHeight);
    if (mCanvas == null && mBitmap == null && mPictureWidth > 0
            && mPictureHeight > 0) {
        mBitmap = Bitmap.createBitmap(mPictureWidth, mPictureHeight,
                Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
        mCanvas.save();

        mRestoreBitmap = Bitmap.createBitmap(mPictureWidth, mPictureHeight,
                Bitmap.Config.ARGB_8888);
        mRestoreCanvas = new Canvas(mRestoreBitmap);
        mRestoreCanvas.save();
    }
    super.onSizeChanged(w, h, oldw, oldh);
}

onDraw:

@Override
protected void onDraw(Canvas canvas) {
    if (mPattern == DRAW_LINE) {
        mCanvas.restore();
        mCanvas.drawPath(path, paint);
        mCanvas.save();
    }
    mCanvas.save();
    if (mBitmap != null)
        canvas.drawBitmap(mBitmap, 0, 0, null);
}

onTouchEvent:

@Override
public boolean onTouchEvent(MotionEvent event) {

    if(event.getAction() == MotionEvent.ACTION_DOWN){
        mRestoreCanvas.drawBitmap(mBitmap, 0, 0, bitmapPaint);
    }

    if (mPattern == DRAW_LINE) {
        return drawLines(event);
    } else if (mPattern == DRAW_PATTERN) {
        return drawPattern(event);
    }
    return false;
}

Undo:

public void dust() {
    path.reset();
    mBitmap.recycle();
    mBitmap = Bitmap.createBitmap(mPictureWidth, mPictureHeight,
            Bitmap.Config.ARGB_8888);
    mCanvas.setBitmap(mBitmap);

    mCanvas.drawBitmap(mRestoreBitmap, 0, 0, bitmapPaint);

    invalidate();
}

Tested OK;



回答4:

Hey I have used a kind of trick to remove the black line.In my erase button, I have set the color to white, instead of using XferMode..

  if(erase){
    paintColor = Color.parseColor(newColor);
    drawPaint.setColor(paintColor);
    }