How to implemement undo features in painting application in Android?

后端 未结 4 1612
情歌与酒
情歌与酒 2021-01-22 12:49

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 drawing

相关标签:
4条回答
  • 2021-01-22 13:43
    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();
        }
    
    0 讨论(0)
  • 2021-01-22 13:49

    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);
        }
    
    0 讨论(0)
  • 2021-01-22 13:52

    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.

    0 讨论(0)
  • 2021-01-22 13:54

    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;

    0 讨论(0)
提交回复
热议问题