random - Using a Handler in combination with an animation with a ViewFlipper -
hello dear programmers!
i trying make animated menu application flips randomly @ random time intervals between 5 viewflipper children. in between each flip, want "insert" animation based on frame frame drawables. far, able display either random flipping or animation based on whether call animation method @ beginning of "run" method or @ end. can't figure out how make sure executed "in between" each iteration of handler.
here code:
public class btg extends activity { private viewflipper fliptest; private handler testhandler = new handler(); private random mrand = new random(); private random timermenu = new random(); int randomtime; animationdrawable menuanimation; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_btg); fliptest = (viewflipper) findviewbyid(r.id.menuflipper); testhandler.postdelayed(mflip, randomtime); } private runnable mflip = new runnable() { @override public void run() { //if call @ beginning, menu flips between 5 first //children of viewflipper , animation never shown startanimation(); randomtime = (timermenu.nextint(6) + 1) * 2000; system.out.println("executes run method " + randomtime); fliptest.setdisplayedchild(mrand.nextint(5)); testhandler.postdelayed(this, (mrand.nextint(6)+ 1) * 2000); //if call @ end, menu displays animation //which launches after random time set in handler //startanimation(); } class starter implements runnable { public void run() { menuanimation.start(); } } private void startanimation() { system.out.println("the start animation method run"); fliptest.setdisplayedchild(5); menuanimation = new animationdrawable(); menuanimation.addframe(getresources().getdrawable(r.drawable.animate_menu_trans1), 100); menuanimation.addframe(getresources().getdrawable(r.drawable.animate_menu_trans2), 100); menuanimation.addframe(getresources().getdrawable(r.drawable.animate_menu_trans3), 100); menuanimation.addframe(getresources().getdrawable(r.drawable.animate_menu_trans1), 100); menuanimation.addframe(getresources().getdrawable(r.drawable.animate_menu_trans2), 100); menuanimation.addframe(getresources().getdrawable(r.drawable.animate_menu_trans3), 100); menuanimation.setoneshot(true); imageview imageview = (imageview) findviewbyid(r.id.menu_animation); imageview.setimagedrawable(menuanimation); imageview.post(new starter()); } };
ok, after playing around , spending hours on stackoverflow, found workable solution problem. here final code. please feel free propose "better" programming alternative newbie fix!
public class btg extends activity { private viewflipper fliptest; private handler menuhandler = new handler(); private random mrand = new random(); private random timermenu = new random(); int randomtime; animationdrawable menuanimation; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_btg); fliptest = (viewflipper) findviewbyid(r.id.menuflipper); menuhandler.postdelayed(mflip, randomtime); } private runnable mflip = new runnable() { @override public void run() { randomtime = (timermenu.nextint(6) + 1) * 2000; system.out.println("executes run method " + randomtime); menuhandler.postdelayed(this, randomtime); startanimation(); } }; class starter implements runnable { public void run() { menuanimation.start(); } } private void startanimation() { system.out.println("the start animation method run"); fliptest.setdisplayedchild(5); menuanimation = new animationdrawable(); menuanimation.addframe(getresources().getdrawable(r.drawable.animate_menu_trans1), 100); menuanimation.addframe(getresources().getdrawable(r.drawable.animate_menu_trans2), 100); menuanimation.addframe(getresources().getdrawable(r.drawable.animate_menu_trans3), 100); menuanimation.addframe(getresources().getdrawable(r.drawable.animate_menu_trans1), 100); menuanimation.addframe(getresources().getdrawable(r.drawable.animate_menu_trans2), 100); menuanimation.addframe(getresources().getdrawable(r.drawable.animate_menu_trans3), 100); menuanimation.setoneshot(true); imageview imageview = (imageview) findviewbyid(r.id.menu_animation); imageview.setimagedrawable(menuanimation); imageview.post(new starter()); thread timer = new thread(){ public void run (){ try{ sleep(700); }catch (interruptedexception e){ e.printstacktrace(); }finally{ system.out.println("the thread sleep works"); runonuithread(new runnable() { @override public void run() { fliptest.setdisplayedchild(mrand.nextint(5)); } }); } } }; timer.start(); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.beat_the_game, menu); return true; }
}
Comments
Post a Comment