当我使用此计时器更改图像时,它无法正常工作。我试图在计时器运行时更改图像。我把这个计时器放在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);
答案 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++;
}
});
}