android - Creating a timer in a Canvas- multithreading -
this code, when app started saw black screen. wanted app add time counter, think multithreading has problems.
timecounter.startcount(); while(pressed!=true){ if (!holder.getsurface().isvalid()) continue; canvas c= holder.lockcanvas(); c.drawbitmap(galaxy, 30, 0, null); c.drawbitmap(player, x-(player.getwidth()/2), y-(player.getheight()/2), null); c.drawtext(timecounter.tostring(), 30, 60, timerpaint); holder.unlockcanvasandpost(c); } }
the timers class(im not sure thread ok):
public class timecounter { private int smallestseconds=0; private int seconds=0; private int minute=0; public timecounter() { smallestseconds=0; seconds=0; minute=0; } public void startcount() { thread t=new thread(); while(pressed!=true) { try{ t.sleep(10); }catch(interruptedexception e){ smallestseconds++; if(smallestseconds==100) { smallestseconds=0; seconds++; if(seconds==60) { seconds=0; minute++; } } } } } public string tostring() { return(minute+":"+seconds+":"+smallestseconds); } }
Comments
Post a Comment