3

I have one class named MyView that extends View..In my options menu i have four types of color.

What i want is when i select green and draw path on canvas it should draw green path and when i select red it should draw red path and previous green should be as it is ...

I got success in drawing multiple paths of various colors but at one time only one color is holding in canvas .. so what can i do to draw multiple color of paths on canvas ??

2
  • Define at one time and include your current code. Commented Feb 20, 2013 at 13:40
  • you'll have to show the code that's drawing on the canvas. basically you'll have to save the color with the path so you can know what color each path is. Commented Feb 20, 2013 at 13:40

1 Answer 1

1

The way i've done this is to store the color along with the path by subclassing Path:

 private class DrawingPath extends Path { public DrawingPath(float w, int c) { Width = w; Color = c; } public float Width; public int Color; } 

Then when i draw each of the paths I have I set the color beforehand.

As you can see you can also set the width with this code so you can change the stroke width for each path.

To draw this i used:

mCanvas.drawColor(Color.WHITE); for (DrawingPath p : mPaths) { mPaint.setColor(p.Color); mPaint.setStrokeWidth(p.Width); mCanvas.drawPath(p, mPaint); } invalidate(); 
Sign up to request clarification or add additional context in comments.

3 Comments

Thnx for answer John . i have only one question that when i change color in that place which code i have to write ?? another way how can i use this DrawingPath class ? Let me know..
I have this code in the onTouchEvent after adding pieces to the path. You would put it anywhere you want to redraw the view.
PLease send me proper example, still i don't understand this whole process.