java - Using a handler to delay a repeated task for a limited number of times -
i have program draws bunch of red boxes in shape of spiral. have working, except cant figure out how delay process can see spiral forming. had code in loop , moved runnable while loop , 500ms delay. program meant stop when onclick or if runs 49 times.
i'm impatient see end result, because phone slows down when start spiral, , has no animation
runnable run = new runnable(){ @override public void run(){ if(row>=0 && (dir==0 && (row==6 || grid[row+1][col]!=null)) || (dir==1 && (col==6 || grid[row][col+1]!=null)) || (dir==2 && (row==0 || grid[row-1][col]!=null)) || (dir==3 && (col==0 || grid[row][col-1]!=null))) dir++; dir=dir%4; switch(dir){ case 0: row++;break; //down case 1: col++;break; //right case 2: row--;break; //up case 3: col--;break; //left } grid[row][col] = new imageview(t); relativelayout.layoutparams layoutparams = new relativelayout.layoutparams(88, 88); layoutparams.setmargins((width/2)-(450)+(88 * row) + (35 * (row + 1))+c, (height/2)-655+(88 * (col+1)) + (35 * (col + 1))+c ,0,0); grid[row][col].setlayoutparams(layoutparams); grid[row][col].setimageresource(r.drawable.redbox); layout.addview(grid[row][col]); x++; c++; c=c%7; while(!stop && x<49) h.postdelayed(run,500); } }; public void spiral() { c=0; row=-1; col=0; dir =0; run.run(); }
there number of ways this, including have done here. first thing remove while()
loop @ end of runnable
, instead have runnable
post long count less 49. have posting same runnable
multiple times each time run, triggered delivered @ same time main handler
.
Comments
Post a Comment