multithreading - Android use thread to draw every 4 seconds -
i quite new android , want create simple game. therefor need thread drawing transparent rectangle on different positions every 4 seconds 2 second break (without drawing). got working "recursive" thread calling new instance of handler.postdelayed. feeling threads tells me, isn't nice way... while searching here similar topics, found out timer construct. can use problem? there better way this?
(edit) thread meant highlighting part of gameboard, 4 seconds. after there should 2 seconds without highlighting. 4 seconds highlighting next part of board etc.
(edit2) couldn't use sleep, because froze ui. if has similar situation, here how solved it:
public class myrunnable implements runnable { private int duration; private int counter; private boolean highlight; public myrunnable(int duration, boolean highlight) { this.duration = duration; this.highlight = !highlight; } @override public void run() { if (highlight) { // highlight 4s long highlight(); invalidate(); mythread = new myrunnable(duration, highlight); postdelayed(mythread, duration); } else { // pause (2s) resethighlight(); invalidate(); mythread = new myrunnable(duration, highlight); postdelayed(mythread, nohighlightduration); } } }
the best way use use invalidate() call ondraw() function , method update position. this-
int x,y; protected void ondraw(canvas canvas) { x=10; y=10; canvas.drawrect(___); update(); invalidate(); } private void update() {/*change x , y/*} here everytime inavlidate() called anywhere ondraw() called again new x , y.
Comments
Post a Comment