Using a counter inside a java.util.Timer in Android Activity causes app to crash -
i'd accomplish think simple, turning out hassle.
i have loading screen picture, , i'd fade in , out application loading. decided accomplish changing it's opacity relative sine value of counter. code follows:
imageview loadingraven; //loading raven @ start of app timer timer; //timer we're gonna have use int elapsed = 0; //elapsed time far /* * following in oncreate() method after contentview has been set */ loadingraven = (imageview)findviewbyid(r.id.imageview1); //fade raven in , out timertask task = new timertask() { public void run() { elapsed++; //this line causes app fail loadingraven.setalpha((float)(math.sin(elapsed)+1)/2); } }; timer = new timer(); timer.scheduleatfixedrate(task, 0, 50);
what problem that's causing program fail? correctly using timer , timertask? or there perhaps better way update opacity of image eases in , out smoothly?
thanks
timertask runs on different thread. update ui on main ui thread. use runonuithread
timertask task = new timertask() { public void run() { elapsed++; runonuithread(new runnable() //run on ui thread { public void run() { loadingraven.setalpha((float)(math.sin(elapsed)+1)/2) } }); } };
timertask runs on different thread. can use handler , postdelayed suggested pskink
Comments
Post a Comment