This is the code to perform undo and redo in Android Canvas and works fine.
package com.example.canva; import java.util.ArrayList; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.view.MotionEvent; import android.view.View; public class MainView extends View { private Canvas mCanvas; private Path mPath; private Paint mPaint; private ArrayList<Path> paths = new ArrayList<Path>(); private ArrayList<Path> undonePaths = new ArrayList<Path>(); public MainView(Context context) { super(context); setFocusable(true); setFocusableInTouchMode(true); mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setDither(true); mPaint.setColor(0xFFFFFFFF); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeJoin(Paint.Join.ROUND); mPaint.setStrokeCap(Paint.Cap.ROUND); mPaint.setStrokeWidth(6); mCanvas = new Canvas(); mPath = new Path(); }
But in my application I need to change the color, stroke width, style, etc., when I try to change the color using the following method, the whole thing in the canvas changes to that color.
public void setColor(int color){ mPaint.setColor(color);}
source share