0

我是 Android 应用程序开发的新手。我只是想要一些关于我想要制作的应用程序的帮助。我正在使用 AsyncTask 来运行计时器,但是在计时器运行后,图像将被更改。

类 syncTask 扩展 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。请帮忙!!

4

2 回答 2

0

更好的是,使用 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)]);
    }       
}
}
于 2013-08-14T14:37:04.027 回答
0

不完全确定这是否有效,但我稍后会在 eclipse 中检查它。将此类放在您的活动中。例子:

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();
    task.execute();
}

private class syncTask extends AsyncTask <Void, Integer, Void> {
    @Override
    protected int doInBackground(Void...params) {
        while(true) {
            publishProgress(random.nextInt(images.length));
            sleep(3000);
        }
    }
    @Override
    protected void onProgressUpdate(Integer...pictureIndex) {
        frame.setImageResource(images[pictureIndex]);
    }
    @Override
    protected void onPostExecute(Void...params) {

    }
}
于 2013-08-14T14:10:27.283 回答