使用计时器更改图像但不工作

时间:2013-10-06 07:50:32

标签: android timer

当我使用此计时器更改图像时,它无法正常工作。我试图在计时器运行时更​​改图像。我把这个计时器放在onCreate上就可以了吗?

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); 

1 个答案:

答案 0 :(得分: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++;
        }
    });
}