When i add a bitmap to an array list the last element is duplicated in previous indexes

Posted by saxofone2 on Stack Overflow See other posts from Stack Overflow or by saxofone2
Published on 2012-11-07T21:55:41Z Indexed on 2012/11/07 23:00 UTC
Read the original article Hit count: 129

Filed under:
|
|

I'm trying to implement a personal way of undo/redo in a finger paint-like app.

I have in synthesis three objects: the Main class (named ScorePadActivity), the relative Main Layout (with buttons, menus, etc, as well as a View object where I create my drawings), and a third object named ArrayList where i'm writing the undo/redo code.

The problem is, when I press the undo button nothing happens, but if I draw anything again "one-time" and press undo, the screen is updated. If I draw many times, to see any changes happen on screen I have to press the undo button the same number of times I have drawn.

Seems like (as in title) when I add a bitmap to the array list the last element is duplicated in previous indexes, and for some strange reason, everytime I press the Undo Button, the system is ok for one time, but starts to duplicate until the next undo. The index increase is verified with a series of System.out.println inserted in code.

Now when I draw something on screen, the array list is updated with the code inserted after the invocation of touchup(); method in motionEvent

touch_up(); }
      this.arrayClass.incrementArray(mBitmap);
    mPath.rewind();
      invalidate();

and in ArrayList activity;

public void incrementArray(Bitmap mBitmap) {
this._mBitmap=mBitmap;
_size=undoArray.size();
    undoArray.add(_size, _mBitmap);
    }

(All Logs removed for clear reading)

The undo button in ScorePadActivity calls the undo method in View activity:

Button undobtn= (Button)findViewById(R.id.undo);
undobtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                mView.undo();
                }
        });

in View activity:

public void undo() {
this.mBitmap= arrayClass.undo();
 mCanvas = new Canvas(mBitmap); 
  mPath.rewind();
    invalidate();
}

that calls the relative undo method in ArrayList activity:

public Bitmap undo() {
    // TODO Auto-generated method stub
    _size=undoArray.size();
                    if (_size>1) {
    undoArray.remove(_size-1);
_size=undoArray.size();
          _mBitmap = ((Bitmap) undoArray.get(_size-1)).copy(Bitmap.Config.ARGB_8888,true);
}
return _mBitmap;
    }

return mBitmap and invalidate:

Due to my bad English I have made a scheme to make the problem more clear: Flux of the problem

I have tried with HashMap, with a simple array, I have tried to change mPath.rewind(); with reset();, new Path(); etc but nothing. Why?

Sorry for the complex answer, i want give you a great thanks in advance. Best regards

© Stack Overflow or respective owner

Related posts about java

Related posts about android