gpt4 book ai didi

android - 停止重复计时器 android

转载 作者:行者123 更新时间:2023-11-29 00:05:39 25 4
gpt4 key购买 nike

我想创建 10 个 ImageViews,它们之间有 5 秒的延迟,我创建了这个代码:

new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
if (counter <= 10)
newimg();
else
// here I want to stop the timer so it will not try to create any more `ImageViews` (the array contains only 10).
}
}, 0, 5000);

private void newimg() {
ball[counter] = new ImageView(this);
ball[counter].setTag(counter);
ball[counter].setBackgroundResource(R.mipmap.ball);
int randomx = rand.nextInt(layoutwidth);
int randomy = rand.nextInt(layoutheight);
ball[counter].setX(randomx);
ball[counter].setY(randomy);
rlt.addView(ball[counter]);
counter++;
}

如何在 else 语句中停止计时器?

最佳答案

调用Timer cancel停止计时器。

关于android - 停止重复计时器 android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33993301/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com