I'm trying to make a redo feature for my app, but I can't figure out what is wrong :/ The idea is to be able to undo up to 10 times, and each time you make a new modification to the image (when you pick you finger up to stop drawing) it shifts all previous images one index to make room for the newest edit and then it saves the edit in the last index of an array of bitmaps. for now I'm just drawing the 3 most recent bitmaps to see if it is working, but all the saved bitmaps are the same. Could I get some advice? Thanks!
float lastPosX; float lastPosY; Bitmap[] previousEdits = new Bitmap[10] public MyView(Context c) { super(c); mBitmap = Bitmap.createBitmap(screenWidth, screenHeight, Bitmap.Config.ARGB_8888); mCanvas = new Canvas(mBitmap); currentPath = new Path(); mBitmapPaint = new Paint(Paint.DITHER_FLAG); for(int i=0;i<previousEdits.length;i++){ //initialize all 10 steps previous edits to be blank bitmaps instead of null previousEdits[i]=Bitmap.createBitmap(screenWidth, screenHeight, Bitmap.Config.ARGB_8888); } } protected void onDraw(Canvas canvas) { canvas.drawColor(Color.WHITE); canvas.drawBitmap(previousEdits[previousEdits.length-1], 0, 0, mBitmapPaint); //draw the most current image canvas.drawBitmap(previousEdits[previousEdits.length-2], 500, 0, mBitmapPaint); //draw second most current canvas.drawBitmap(previousEdits[previousEdits.length-3], 500, 0, mBitmapPaint); //draw third most current } public boolean onTouchEvent(MotionEvent event) { float x = event.getX(); float y = event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: 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; } private void touch_start(float x, float y) { currentPath.moveTo(x, y); lastPosX = x; lastPosY = y; } private void touch_move(float x, float y) { float dx = Math.abs(x - lastPosX); float dy = Math.abs(y - lastPosY); currentPath.quadTo(lastPosX, lastPosY, (x + lastPosX)/2, (y + lastPosY)/2); lastPosX = x; lastPosY = y; } private void touch_up() { // commit the path to our offscreen if(canvasInUse) { mCanvas.drawPath(currentPath, drawingPaint); } // kill this so we don't double draw currentPath.reset(); //update the previous bitmaps updateLatestEdits(mBitmap); } void updateLatestEdits(Bitmap latestEdit){ for(int i =0;i<previousEdits.length-1;i++){ //shift bitmaps to make room for new edit previousEdits[i]=previousEdits[i+1]; } previousEdits[previousEdits.length-1]=latestEdit; //put in new edit at the end }