java - how do i add a timer to make the line refresh and appear after evry 2 seconds? -
i making app make hands of clock.that is, draw simple line on screen , want rotate hands of clock.how do using timer?(so position of line refreshes).only 1 end point of line should rotate,while other remains staionary.in folllowing code,i draw line on screen:
drawview.java:
package linerefresh.xyz.com; import java.util.timer; import java.util.timertask; import android.content.context; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.view.view; public class drawview extends view { paint paint = new paint(); public drawview(context context) { super(context); } @override public void ondraw(final canvas canvas) { paint.setcolor(color.black); canvas.drawline(50, 200, 270, 200, paint); }
}
linerefresh.java:
package linerefresh.xyz.com; import android.app.activity; import android.graphics.color; import android.os.bundle; public class linerefresh extends activity { drawview drawview; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); drawview = new drawview(this); drawview.setbackgroundcolor(color.white); setcontentview(drawview); } }
try this:
private handler mhandler = new handler();
private runnable methodtodrawclockline = new runnable() { public void run() { //execute here drawing routine. // after drawing set timer x period of time mhandler.postdelayed(methodtodrawclockline, 1000); //it'll re-execute after 1sec. } }
in oncreate or method want start timer can use:
mhandler.removecallbacks(mupdatetimetask); //be sure remove handler before add new one
mhandler.postdelayed(methodtodrawclockline, 1000); //it'll execute after 1sec.
when you're done, remember remove handler. mhandler.removecallbacks(mupdatetimetask);
that shold it.
Comments
Post a Comment