I use functions for canvas like drawCircle and drawPoint in android.
This works fine.
But the problem now is to draw these different items with a delay, so it looks like an animation.
What kind of mechanism should I use? Have tried with async but I dont like that way of doing it.
Should I use some kind of timer that just draw with an interval or is there other clever ways to do this?
I use this strategy, first I declare a Handler and a Runnable that way:
Then when I want to start my time manager I just call the mTimeManager.run() and it will start to notify my
Observers (previously added) periodically.If you need for some reason stop the timer or something you just do that:
[ EDIT – More complete code ]
Ok than let’s make it clearer, first I made a custom Observable object like that [that’s optional]:
the reason for that is just because I can’t call setChanged() outside Observable class – it’s protected, if it’s not changed it doesn’t notify any observer.
The other declarations keep the same as shown before, now I need to start this
TimeManagersomewhere, my app is a LiveWallpaper and I make all rendering stuff into a class that extends aThreadbut you don’t need that necessarily, I made a method calledresumeDrawing(), this one is called right aftersuper.start();at my@Overrideofpublic synchronized void start()fromThreadclass, the method looks like that:and it’s dual:
Ok, now we can start and stop the time manager, but who’s listening? Nobody! so let’s add’em: On the constructor of my Renderer I add some
Observers to mymObservableobject, one of those is the Renderer itself, so my renderer extendsThreadand implementsObserver:to add observers you simply do
mObservable.addObserver(THE_OBJECT - Implements Observer)you can see that I don’t re-render my stuff each time I’m notified, that’s because I use this TimeManager for other thinks than just refresh the
Canvaslike updating the position of the objects I want to draw just internally.So, what you need to slow down the drawing is to change the way your objects change internally while the time passes, I mean your circles and points etc, or you can chance your time step, I recommend the first one.
Was it clearer? I hope it helps.