Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to perform Undo and Redo operation on canvas in android
    primarykey
    data
    text
    <p>How can I perform the Undo/Redo operation on canvas . And my code is blow and i am not performing the these two operation .In the main class paint is performing by this reason am not perform the undo redo operation.</p> <pre><code>public class MainActivity extends Activity implements OnClickListener{ MyView mView; private Path mPath; Button btnUndo,btnRedo; FrameLayout frmLayout; private Paint mPaint; private MaskFilter mEmboss; Bitmap mBitmap; int bckcolor=0xffffffff; final Context context = this; ArrayList&lt;Path&gt; paths = new ArrayList&lt;Path&gt;(); ArrayList&lt;Path&gt; undonePaths = new ArrayList&lt;Path&gt;(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mView = new MyView(this); setContentView(R.layout.activity_main); btnUndo=(Button)findViewById(R.id.Undo); btnRedo=(Button)findViewById(R.id.Redo); btnUndo.setOnClickListener(this); btnRedo.setOnClickListener(this); frmLayout=(FrameLayout)findViewById(R.id.frameLayout); frmLayout.addView(mView,LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); mPaint = new Paint(); mPath=new Path(); mPaint.setAntiAlias(true); mPaint.setDither(true); mPaint.setColor(0xFF000000); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeJoin(Paint.Join.ROUND); mPaint.setStrokeCap(Paint.Cap.ROUND); mPaint.setStrokeWidth(5); new Canvas(); paths.add(mPath); mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 },0.4f, 6, 3.5f); new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.Undo: if (paths.size()&gt;0) { undonePaths.add(paths.remove(paths.size()-1)); mView.invalidate(); Toast.makeText(getApplicationContext(), "Undo", 1000).show(); } mView.undo(); break; case R.id.Redo: mView.redo(); break; default: break; } } public class MyView extends View { private float posX = 105; private float posY = 105; Mode mode; 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;(); BlurMaskFilter blurMaskFilter = new BlurMaskFilter(10, BlurMaskFilter.Blur.OUTER); int screenWidth; int screenHeight; /** */ public MyView(Context c) { super(c); mBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.e); mBitmap = Bitmap.createBitmap(800, 800, Bitmap.Config.ARGB_8888); mCanvas = new Canvas(mBitmap); mPath = new Path(); mBitmapPaint = new Paint(Paint.DITHER_FLAG); mCanvas.drawColor(0xFFFFFFFF); paths.add(mPath); } public void redo() { // TODO Auto-generated method stub if (undonePaths.size()&gt;0) { paths.add(undonePaths.remove(undonePaths.size()-1)); invalidate(); } else { } } public void undo() { // TODO Auto-generated method stub 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(); } } @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.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 drawImage (int resourceId) { Bitmap _scratch = BitmapFactory.decodeResource(getResources(), resourceId); mCanvas.drawBitmap(_scratch, 0, 0, null); } } } </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