1

I have an android application that draws a path of points in an arraylist (named 'test'). An algorithm updates the arraylist to make the path shorter each time (it uses the Travelling Salesman Problem algorithm). The problem I am facing at the moment is that the previous path does not clear, instead the new path draws over the previous one.

//this class draws a line
public void CompDrawLine(List test) {
    // int d = 0;
    int i=0;
    test.add(test.get(0));
    Point c = test.get(i);

    for (i=0;i<(test.size()-1);i++) {
        cPath.moveTo(c.x,c.y);
        c = test.get(i+1);
        cPath.lineTo(c.x,c.y);
        mCanvas.drawPath(cPath,cPaint);

        //cPath.reset();
    }

    // cPath.reset();
    invalidate();
}
antonio
  • 18,044
  • 4
  • 45
  • 61

1 Answers1

1

To clear your Canvas you need to draw on it, for example:

mCanvas.drawColor(Color.WHITE);

If you need to use a previous state of the `Canvas' you will need to implement some logic, for example using a Memento pattern

Edit:

Using a Memento pattern on a Canvas object can be hard to manage and lead to performance issues.

Using a Memento pattern on a Bitmap object can also lead to performance or memory issues depending on the size of the bitmap.

In this case (as you can see in the comments), @Amna Hussain solved the issue drawing the same path again in white color to erase the previous drawn path as a workaround.

antonio
  • 18,044
  • 4
  • 45
  • 61
  • I have drawn on the canvas "mPath.drawPath" – Amna Hussain Mar 10 '15 at 14:28
  • @Amna Hussain I mean that you need to draw on the entire `Canvas`to clear it (that's what the `drawColor()` function does). You can't "undraw" the paths, once you have drawn, you can only draw over it to clear it. That's why I propose you to use `drawColor()` or to write some code to save the `Canvas` state. – antonio Mar 10 '15 at 15:34
  • thank you I see what you mean. tbh i need a quick fix as this will be the final stage of my project and im running out of time. I have tried to draw over the previous path as suggested but the results are a bit weird. do you have any suggested code? – Amna Hussain Mar 10 '15 at 17:45
  • if i was to use the momento pattern would I save in the state where the path has not been drawn yet and then restore back to that point once I want the new path to be drawn? – Amna Hussain Mar 11 '15 at 12:50
  • @Amna Hussain I have not implemented a functionality like this so I don't have the code, but yes, you need to save the state of the `Canvas` before you draw on it and restore that state when you need to draw again – antonio Mar 11 '15 at 12:59
  • @ antonio i have tried this using canvas.save() at the point i want to save the canvas and canvas.restore() when I want to restore it for the next path to be drawn and it doesn't work – Amna Hussain Mar 11 '15 at 13:06
  • @Amna Hussain I have done a quick test and maybe saving the `Canvas` state is not the best solution in terms of speed of execution and simplicity. If you are drawing on a `Bitmap` maybe it's better to save this `Bitmap`'s state – antonio Mar 11 '15 at 13:50
  • Yes!! We are on the same page now, I have tried to look this up so instead of saving the canvas i save the bitmap. Do you know how to do this? – Amna Hussain Mar 11 '15 at 13:57
  • I have tried to save the bitmap with no luck. Could there be a way to go over the path again but in white to make it disappear theoretically? – Amna Hussain Mar 11 '15 at 17:22
  • @Amna Hussain You can save the `Bitmap` but eventually you can find a performance problem. Yes, what you propose is a workaround (drawing the path again in white). You can change the `Paint` color in your case by doing `cPaint.setColor(Color.WHITE);` and then `mCanvas.drawPath(cPath,cPaint);` with the old path before painting the new path – antonio Mar 11 '15 at 17:29
  • Dear Antonio, this is what I've done: for (i=0;i<(test.size()-1);i++) cPaint.setColor(Color.WHITE); cPath.reset(); cPath.moveTo(c.x, c.y); c = test.get(i + 1); cPath.lineTo(c.x, c.y); mCanvas.drawPath(cPath, cPaint); }invalidate(); } this happens way too fast so the user doesnt even see the red path happening. I'm thinking to implement a 5 seconds delay somehow between the two paths of different colours and for the final path the tsp algorithm to be in red. – Amna Hussain Mar 11 '15 at 17:51
  • @Amna Hussain If this solved your issue, I'm going to edit my answer adding all the info, so if another person reaches this thread can easily find the solution that worked for you – antonio Mar 11 '15 at 17:59
  • It worked! I used a handler: for (i[0] =0; i[0] <(test.size()-1); i[0]++){cPath.reset(); cPath.moveTo(c[0].x, c[0].y); c[0] = test.get(i[0] +1); cPath.lineTo(c[0].x, c[0].y);mCanvas.drawPath(cPath,cPaint); } mHandler.postDelayed(new Runnable() { public void run() {or (i[0] =0; i[0] <(test.size()-1); i[0]++) {cPaint.setColor(Color.WHITE);cPath.reset();cPath.moveTo(c[0].x, c[0].y); c[0] = test.get(i[0] + 1);cPath.lineTo(c[0].x, c[0].y);mCanvas.drawPath(cPath, cPaint);} }},5000); invalidate();} – Amna Hussain Mar 12 '15 at 15:09