How to draw the multiple lines with different color and undo,redo the paths in android?
android, bitmap, java
Solution
When you handle the MotionEvent.ACTION_UP : you need to save the color used to draw the path with something like this :
case MotionEvent.ACTION_UP:
paths.add(mPath);
colorsMap.put(mPath,selectedColor); // store the color of mPath
...
Before drawing a path, you need to set the paint color:
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
for (Path p : paths)
{
mPaint.setColor(colorsMap.get(p));
canvas.drawPath(p, mPaint);
}
mPaint.setColor(selectedColor);
canvas.drawPath(mPath, mPaint);
}
And the colorsMap is a simple instance variable:
private Map<Path, Integer> colorsMap = new HashMap<Path, Integer>();
To implement the Undo/Redo feature, you just have to remove the last element from paths (and store it in a undoneList so that on redo can restore it). see Android Canvas Redo and Undo Operation
Problem
I want to draw multiple lines on the view with different colors and undo,redo the paths in android. I use the bitmap paint option, each path has a unique color but undo,redo is not working. Here is my code of bitmappaint: ``` public MyView(Context context, Object object) { super(context); setFocusable(true); setFocusableInTouchMode(true); mPath = new Path(); mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setColor(0xFFFFFF00); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeJoin(Paint.Join.ROUND); mPaint.setStrokeCap(Paint.Cap.ROUND); mPaint.setStrokeWidth(3); mBitmap = Bitmap.createBitmap(320, 480, Bitmap.Config.ARGB_8888); mCanvas = new Canvas(mBitmap); } protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); } protected void onDraw(Canvas canvas) { canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint); for (Path p : paths) { canvas.drawPath(p, mPaint); } canvas.drawPath(mPath, mPaint); } public boolean onTouchEvent(MotionEvent event) { float x = event.getX(); float y = event.getY(); int action = event.getAction(); int action1=event.getAction(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: undonePaths.clear(); mPath.reset(); mPath.moveTo(x, y); mX = x; mY = y; startPoint = new PointF(event.getX(), event.getY()); endPoint = new PointF(); invalidate(); // isDrawing = true; break; case MotionEvent.ACTION_MOVE: float dx = Math.abs(x - mX); System.out.println("action move"); float dy = Math.abs(y - mY); if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) { // currentDrawingPath.path.quadTo(mX,mY,(x + mX)/2, (y + mY)/2); } mX = x; mY = y; endPoint.x = event.getX(); endPoint.y = event.getY(); isDrawing = true; invalidate(); break; case MotionEvent.ACTION_UP: mPath.lineTo(mX, mY); paths.add(mPath); mPath = new Path(); // mCanvas.drawPath(mPath, ppaint); endPoint.x = event.getX(); endPoint.y = event.getY(); isDrawing = false; invalidate(); break; default: break; } } ``` without bitmap using i faced the color problem if i select a blue color for a path means all the previous paths will be changed to blue color; Here is my code ``` protected void onDraw(Canvas canvas) { canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint); for (Path p : paths) { canvas.drawPath(p, mPaint); } canvas.drawPath(mPath, mPaint); } ``` Can anyone help me to draw multiple paths with different color of unique paths in android?