java - Threading issues with animation -
so, know it's bad practice ever sleep ui thread. i've got issue here though , i'm confused how around it. have loop executes inside of ui thread. 1 of methods (placetile()
) has animation associated it. because it's loop, need pause code execution give time animation complete , allow user see happened. (example, dealing or drawing cards. should see each tile placed 1 @ time can follow flow of board game.) but, since android api must run on ui thread, (meaning animation can't happen on worker thread), how supposed achieve this? if pause ui thread, animation won't run, right? can't run animation on worker thread. , since loop includes animation, mean can't pause loop , allow animation complete?
i'm still pretty new threading forgive me if i'm missing obvious.
here's code:
public void setupplayers(){ //toast setting turn order smalltoast(getresources().getstring(r.string.deciding_turn_order)); (int = 0; < players.length; i++) { if(i == 0){ players[i] = new player("human", 6000); players[i].settype(player.type.human); } else { players[i] = new player(playernames[i], 6000); players[i].settype(player.type.computer); } players[i].drawtile(1); textview nametext = (textview) findviewbyid(r.id.current_player_text); nametext.settext(players[i].getname()); //find id tile placed string tileid = players[i].findtileidbyindex(0); placetile(tileid, i); //tile id being placed, , index of player placing //todo: pause , wait animation complete } }
i know it's bad practice ever sleep ui thread
.
correct, it's very bad practice , should avoid possible. can achieve running thread
process you're supposed run within main ui , update main ui declaring handler
.
the steps are, declare handler
within main ui, access handler
inside thread
loop inside class, , whenever need update in main ui
, send message
ui thread
.
static class myhandler extends handler { @override synchronized public void handlemessage(final message msg) { final string mymessage = msg.tostring(); // whatever need do... ... } } final myhandler handler = new myhandler(); final thread lowp = new thread( new runnable() { public void run() { // whatever need ... // , call when have in main ui final message mess = new message(); mess.obj = "update ui!"; handler.sendmessage(mess); } }); myloop.start();
Comments
Post a Comment