Newb--Q: HOW to DRAW with worker THREAD

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Newb--Q: HOW to DRAW with worker THREAD

Postby doubleslash » Sat Feb 21, 2009 5:23 pm

Hi,
I'm writing a program to draw one circle at a time onto SurfaceView using a secondary thread. It runs fine, but I would see multiple circles at a time. Can you please help as I'm going insane trying to understand where the bug could be. Inside the class which implments Runnable, I have the run method continuously draw different circles and post to the surface one at a time (or at least I think so).
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2.  
  3. public void run(){
  4.  
  5. try {
  6.  
  7.                                 while(!done){
  8.  
  9. //done is set to be false so we can continuously draw onto the SurfaceView until quitJob() is called.
  10.  
  11.                                 //code to change drawing_r here
  12.  
  13.                                         synchronized(mholder){
  14.  
  15.                                                 c = mholder.lockCanvas();
  16.  
  17.                                                 c.drawCircle(center.x, center.y, drawing_r, mbrush);
  18.  
  19.                                                 mholder.unlockCanvasAndPost(c);
  20.  
  21.                                                 }
  22.  
  23.                                         Thread.sleep(2000);
  24.  
  25.                                        
  26.  
  27.                                 }
  28.  
  29.                         }catch (InterruptedException ex){}
  30.  
  31. }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

Inside the class which extends SurfaceView, I have the following resume and pause methods which are called by SurfaceCreated and SurfaceDestroyed, respectively.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         public void resume(){
  2.  
  3.                 //start the background drawing job
  4.  
  5.                 if(jobThread != null){
  6.  
  7.                         pause();
  8.  
  9.                 }
  10.  
  11.                 job = new bckGrndJob(mholder.getSurfaceFrame().width(), mholder.getSurfaceFrame().height());
  12.  
  13.                 //task
  14.  
  15.                 jobThread = new Thread(job, "draw");
  16.  
  17.                 jobThread.start();
  18.  
  19.                
  20.  
  21.         }
  22.  
  23.         public void pause(){
  24.  
  25.                 //stop the thread/job
  26.  
  27.                 job.quitJob(); //this will set done to be true and allows the Runnable run method above to exit the while loop
  28.  
  29.                 if(jobThread != null){
  30.  
  31.                                 try {
  32.  
  33.                                         jobThread.join();
  34.  
  35.                                 } catch (InterruptedException e) {}
  36.  
  37.                 }
  38.  
  39.                 jobThread = null;
  40.  
  41.         }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4

Thank you for looking.
doubleslash
Developer
Developer
 
Posts: 30
Joined: Fri Feb 13, 2009 6:05 am

Top

Postby MrSnowflake » Sun Feb 22, 2009 1:11 pm

Have you taken a look at the LunarLander sample?
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby doubleslash » Mon Feb 23, 2009 6:23 am

Thanks for the reply. It turned out that I just needed to clear the canvas.
doubleslash
Developer
Developer
 
Posts: 30
Joined: Fri Feb 13, 2009 6:05 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Exabot [Bot] and 3 guests