Android FingerPaint Undo/Redo implementation

后端 未结 5 1265
庸人自扰
庸人自扰 2020-12-28 21:48

I\'m working on a test project which is something similar to FingerPaint example in Android SDK Demos. I was trying to implement undo/redo functionality in my p

5条回答
  •  Happy的楠姐
    2020-12-28 22:07

    I don't know if this is what you had in mind but it's how i am doing it. Instead of storing it in only one path, you store an array with all the paths, like this the user can draw many lines, with a small modification you can add multi touch too.

    To make the undo and redo, just remove or add the last path path from the paths variable and store them in a new array. Something like:

    public void onClickUndo () { 
        if (paths.size()>0) { 
           undonePaths.add(paths.remove(paths.size()-1))
           invalidate();
         }
        else
         //toast the user 
    }
    
    public void onClickRedo (){
       if (undonePaths.size()>0) { 
           paths.add(undonePaths.remove(undonePaths.size()-1)) 
           invalidate();
       } 
       else 
         //toast the user 
    }
    

    Here is my modified panel, I cant try it right now but the methods above should work! Hope it helps! (there are few extra variables just remove them :)

    private ArrayList undonePaths = new ArrayList(); 
    public class DrawingPanel extends View implements OnTouchListener {
    
    private Canvas  mCanvas;
    private Path    mPath;
    private Paint   mPaint,circlePaint,outercirclePaint;   
    private ArrayList paths = new ArrayList();
    private ArrayList undonePaths = new ArrayList(); 
    private float xleft,xright,xtop,xbottom;
    
    public DrawingPanel(Context context) {
        super(context);
        setFocusable(true);
        setFocusableInTouchMode(true);
    
        this.setOnTouchListener(this);
    
    
        circlePaint = new Paint();
        mPaint = new Paint();
        outercirclePaint = new Paint();
        outercirclePaint.setAntiAlias(true);
        circlePaint.setAntiAlias(true);
        mPaint.setAntiAlias(true);        
        mPaint.setColor(0xFFFFFFFF);
        outercirclePaint.setColor(0x44FFFFFF);
        circlePaint.setColor(0xAADD5522);
        outercirclePaint.setStyle(Paint.Style.STROKE);
        circlePaint.setStyle(Paint.Style.FILL);        
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(6);
        outercirclePaint.setStrokeWidth(6);        
        mCanvas = new Canvas();
        mPath = new Path();
        paths.add(mPath);             
    
    
        cx = 400*DrawActivity.scale;
        cy = 30*DrawActivity.scale;
        circleRadius = 20*DrawActivity.scale;
        xleft = cx-10*DrawActivity.scale;
        xright = cx+10*DrawActivity.scale;
        xtop = cy-10*DrawActivity.scale;
        xbottom = cy+10*DrawActivity.scale;
    
    }
    
    
    public void colorChanged(int color) {
        mPaint.setColor(color);
    }
    
    
        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w, h, oldw, oldh);
        }
    
        @Override
        protected void onDraw(Canvas canvas) {            
    
            for (Path p : paths){
                canvas.drawPath(p, mPaint);
            }
    
        }
    
        private float mX, mY;
        private static final float TOUCH_TOLERANCE = 0;
    
        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);
            // commit the path to our offscreen
            mCanvas.drawPath(mPath, mPaint);
            // kill this so we don't double draw            
            mPath = new Path();
            paths.add(mPath);
        }
    
    
    
    @Override
    public boolean onTouch(View arg0, MotionEvent event) {
          float x = event.getX();
          float y = event.getY();
    
          switch (event.getAction()) {
              case MotionEvent.ACTION_DOWN:
                  if (x <= cx+circleRadius+5 && x>= cx-circleRadius-5) {
                      if (y<= cy+circleRadius+5 && cy>= cy-circleRadius-5){
                          paths.clear();
                          return true;
                          }
                  }
                  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;
    }
    
    
    
    
    }
    

提交回复
热议问题