canvas - Android: how to SurfaceView multiple drawings on cavas with no drawing sync -
i'm trying simple app changes cavas background colour every 500ms, on cavas create n circles each vary radius every x millisecond.
how can if sleep time in "run()" method dictated cavas colour change. should create new thread every circle , sync of them?
cleary need take in consideration circles have drawn after canvas background color change, since risk circles not visible due background layer drawn obove circles?
for kind of job should consider working opengl?
this run():
public void run() { int i=0; paint paint= new paint(); paint.setcolor(color.red); log.d("zr", "in running"); while(running){ try { thread.sleep(500); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } if(!holder.getsurface().isvalid()) continue; canvas canvas = holder.lockcanvas(); canvas.drawrgb(rand.nextint(255), rand.nextint(255), rand.nextint(255)); canvas.drawcircle(canvas.getwidth()/2, canvas.getheight()/2, 100, paint); holder.unlockcanvasandpost(canvas); log.d("zr", "in running: "+i +" count: "+j); i++; j++; } }
an alternative using thread.sleep()
implement timer trigger different drawing routines. here pseudocode:
timeoflastbackgroundchange = currentsystemtime() timeoflastcircleresize = currentsystemtime() needscanvasredraw = false while(running) { if (currentsystemtime() - timeoflastbackgroundchange > 500) { changebgcolor() timeoflastbackgroundchange = currentsystemtime() needscanvasredraw = true } if (currentsystemtime() - timeoflastcircleresize > n) { resizecircle() timeoflastcircleresize = currentsystemtime(); needscanvasredraw = true } if (needscanvasredraw) { drawupdatedobjects() needscanvasredraw = false }
basically, within loop, keep track of last time changed background color , resized circles. in every iteration of loop, check if enough time has elapsed warrant background change or circle resize. if has, make change , record current time of change can record elapsed time next change. needscanvasredraw
flag lets redraw when has changed rather every loop iteration.
Comments
Post a Comment