Skip to content
Advertisement

Drawing a graph in Paint with Android graphics

I’m trying to plot a graph on a View using a canvas. So far so good it plots correctly as show in the next image:

Desired Output

once you tap on the screen the expected result should clear the screen and plot a new graph but instead draws on top of it resulting in this:

Actual Output

JavaScript

How do I properly clear screen?

I have tried

JavaScript

seen from other questions but it does not work.

Advertisement

Answer

Actually it does not have anything to do with your clear color – the screen gets cleared correctly. The problem is the xPosList and likewise the yPosList ArrayList.

As soon as the user taps the screen the generateData() function gets called which keeps adding elements to both ArrayList. But: you never clear those! So as soon as you tap the screen twice, the ArrayList’s contain random data from the first and the second tap.

You need to clear those right before the for-loop:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement