Hello have only a few days with Java and android here. I am a bit confused about exactly how the “implements runnable” actually works example:
public class DrawableSurfaceView extends SurfaceView implements Runnable {
[...]
public void resume(){
isRunning = true;
mThread = new Thread(this);
mThread.start(); //start the animation
parseParameters(); //<== Here is my problem
}
public void run() {
while (isRunning == true){
if (!mHolder.getSurface().isValid()) {
continue;
}
Canvas canvas = mHolder.lockCanvas();
canvas.drawARGB(255, 0, 0, 0);
canvas.drawPath(PenPath, PenPaint);
canvas.drawPath(CursorPath, CursorPaint);
mHolder.unlockCanvasAndPost(canvas);
}
}
public void parseParameters() {
[...]
[ The rest of my code here modifying PenPath and CursorPath, etc ]
}
I am embarrassed to ask, but I thought that after mThread.start(); a new thread would be started running a loop in the run method. instead what I get is the run method only executed after my parseParameters() method terminates.
What I wanted to achieve is to have the canvas on a drawing loop thread and externally modify the parameters of the drawing paths to generate my animation.
I am sure this is very elemental, but I have been unable to understand this for a few hours. The docs are not helping.
Any pointer would help a lot. Cheers guys!
It seems that in this situation you would not want to implement any kind of loop yourself. You would want to override the onDraw(Canvas canvas) in your custom view. With this you can force onDraw to be called by calling invalidate() on your view from anywhere. You could accept parameters from an outside source as well.