Note that there are some explanatory texts on larger screens.

plurals
  1. POhow perform undo redo on canvas without losing previous color
    primarykey
    data
    text
    <p>Am not performing Undo/Redo operation canvas in this code. When am use the array list for removing the postion then it not work and during changing the color of paint from color picker the previous drawing color has been also change. I am not understanding that how to perform UNDO/REDO for this. So plz help</p> <p>//This is main class</p> <pre><code>protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView (mView = new MyView(this)); mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setDither(true); mPaint.setColor(0xFFFF0000); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeJoin(Paint.Join.ROUND); mPaint.setStrokeCap(Paint.Cap.ROUND); mPaint.setStrokeWidth(4); mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 }, 0.4f, 6, 3.5f); mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL); </code></pre> <p>}</p> <p>//And MyView class is</p> <pre><code>public class MyView extends View { private static final float MINP = 0.25f; private static final float MAXP = 0.75f; private Bitmap mBitmap; private Canvas mCanvas; private Path mPath; private Paint mBitmapPaint; ArrayList&lt;Path&gt; paths = new ArrayList&lt;Path&gt;(); ArrayList&lt;Path&gt; undonePaths = new ArrayList&lt;Path&gt;(); public MyView(Context c) { super(c); mBitmap = Bitmap.createBitmap(320, 480, Bitmap.Config.ARGB_8888); mCanvas = new Canvas(mBitmap); mPath = new Path(); mBitmapPaint = new Paint(Paint.DITHER_FLAG); // mCanvas.setBackgroundResource(0xFFFFFFFF); mCanvas.drawColor (0xFFFFFFFF); paths.add(mPath); } public MyView (Context c, int color) { super(c); mBitmap = Bitmap.createBitmap(320, 480, Bitmap.Config.ARGB_8888); mCanvas = new Canvas(mBitmap); mPath = new Path(); mBitmapPaint = new Paint(Paint.DITHER_FLAG); mCanvas.drawColor (color); paths.add(mPath); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); } @Override protected void onDraw(Canvas canvas) { canvas.drawColor(0xFF000000); canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint); for(Path p: paths) { canvas.drawPath(p, 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 &gt;= TOUCH_TOLERANCE || dy &gt;= 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); paths.add(mPath); mPath.reset(); } @Override 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; } public void clear () { Paint p = new Paint(); p.setAntiAlias(true); p.setDither(true); p.setColor(0xFFFFFFFF); mCanvas.drawPaint (p); } public void drawImage (int resourceId) { Bitmap _scratch = BitmapFactory.decodeResource(getResources(), resourceId); mCanvas.drawBitmap(_scratch, 0, 0, null); } public void undo() { // TODO Auto-generated method stub mCanvas.drawCircle(posX, posY, 100, mPaint); Toast.makeText(getApplicationContext(), "Hello", 1000).show(); if (paths.size()&gt;0) { undonePaths.add(paths.remove(paths.size()-1)); mView.invalidate(); Toast.makeText(getApplicationContext(), "Undo", 1000).show(); } else { Toast.makeText(getApplicationContext(), "Not", 1000).show(); } } public void redo() { // TODO Auto-generated method stub if (undonePaths.size()&gt;0) { paths.add(undonePaths.remove(undonePaths.size()-1)); invalidate(); } else { } } public void saveAsJpg (File f) { String fname = f.getAbsolutePath (); FileOutputStream fos = null; try { fos = new FileOutputStream (f); mBitmap.compress (CompressFormat.JPEG, 95, fos); } catch (Throwable ex) { ex.printStackTrace (); } } // end saveAsJpg } // end MyView } // end class FingerPaint </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload