我只是想用計時器改變我畫的圓的顏色。我已經實現下面的代碼到我的「onCreate
」的方法:用計時器改變圈的顏色
Timer t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
Drawing.switchColor();
}
});
}
},
1000,
1000);
switchColor()
執行以下操作的方法:
public static void switchColor() {
Random r = new Random(30);
int random = r.nextInt();
if(random < 10) {
p.setColor(Color.GREEN);
}
else if(random >10 && random < 20) {
p.setColor(Color.BLUE);
}
else {
p.setColor(Color.RED);
}
}
當我運行此,顏色停留在它的默認。
有誰知道我是否必須使用一個處理程序或不同的計時器模型?
提前致謝!
您將問題的標題更改爲「已解決」,請將其更改回原來的位置,並在解決問題的答案旁邊打鉤。 (如果這是你自己的答案,這沒有關係) – Frakcool
好的,謝謝,我會的! – Jolle
謝謝!所有你需要做的就是接受你的答案,你完成了C:(我想你必須等待1或2個小時左右,所以我不記得了)如果我把「t.start();」放入「 – Frakcool