0

When i using this timer to change the image, its not working. I am trying to change the image when the timer run.I put this timer inside onCreate is that okay?

Timer timer = new Timer("MetronomeTimer", true);
    TimerTask tone = new TimerTask() {

        int i=0;
        @Override
        public void run() {

            img1.setImageResource(Images[i]);
            i++;

        }
    };
    timer.scheduleAtFixedRate(tone, 500, 500); 
4

1 回答 1

0

尝试这样的事情:

private Handler handler = new Handler();
handler.postDelayed(runnable, 100);

private Runnable runnable = new Runnable() {
@Override
public void run() {
/* do what you need to do */
changeImage();
/* and here comes the "trick" */
handler.postDelayed(this, 100);
}
};

changeImage可以是类似的东西

void changeImage()
{
  runOnUiThread(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            img1.setImageResource(Images[i]);
            i++;
        }
    });
}
于 2013-10-06T08:53:20.207 回答