安卓:更改与AsyncTask的定时和随机图像 [英] Android: Change Random images with AsyncTask and Timer
本文介绍了安卓:更改与AsyncTask的定时和随机图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是一个新手,Android应用程序开发。我只是想一些帮助,我想使应用程序。
我正在使用的AsyncTask运行一个计时器,但计时器运行后,所述图像将被改变。
I am a newbie to Android App Development. I just want some help with the app I want to make. I am using an AsyncTask to run a Timer,but after the timer runs, the image is to be changed.
类syncTask扩展的AsyncTask
{
class syncTask extends AsyncTask {
@Override
protected Void doInBackground(Void... arg0)
{
Thread timer=new Thread(){
public void run(){
try{
sleep(3000);
}
catch(InterruptedException e){
e.printStackTrace();
}
}
};
timer.start();
return null;
}
@Override
protected void onPostExecute(Void result)
{
q=images[random.nextInt(images.length)];
frame.setImageResource(q);
w=q;
}
}
我能做些什么来更新ImageView的。请帮助!
What can I do to update the imageview. Please Help!!
推荐答案
更妙的是,使用一个TimerTask:
Even better, use a TimerTask:
public class AndroidActivity extends Activity {
private ImageView frame;
private Image[] images = {blah, blah, blah};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
frame = (ImageView)findViewById(R.id.frame);
syncTask task = new syncTask();
Timer timer= new Timer();
task.schedule(timer, 0, 3000);;
}
private class syncTask extends TimerTask {
public void run() {
frame.setImageResource(images[random.nextInt(images.length)]);
}
}
}
这篇关于安卓:更改与AsyncTask的定时和随机图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文