我試圖執行一個基本的任務,旋轉畫布20秒,使用計時器 秒,但它似乎並沒有正常工作,其 落後。例如,如果我每50毫秒將矩形旋轉0.3度,則它應該在第二秒內旋轉6度,但情況並非如此。它 旋轉真的很慢。這裏是我的示例代碼:計時器任務沒有準確執行
//Code for update task
class UpdateTimeTask extends TimerTask {
public void run() {
hndView.post(new Runnable() {
public void run() {
hndView.invalidate(); //this code invalidates custom view that calls onDraw to draw rotated hand
}
});
}
}
//Code for onDraw method of custom view that needs to be update
@Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
//ang is angle to rotate and inc is float value of 0.3 degree to be incremented
ang = ang + inc;
if (ang >= 360) ang = ang - 360;
canvas.rotate(ang, canvas.getWidth()/2, canvas.getHeight()/2);
canvas.drawRect((canvas.getWidth()/2 - 2), (canvas.getHeight()/2 - 125), (canvas.getWidth()/2 + 2), (canvas.getHeight()/2 + 10), mTextPaint);
canvas.restore();
}
//code to schedule task
Timer timer = new Timer();
UpdateTimeTask tt = new UpdateTimeTask();
timer.schedule(tt, 0, 50);
任何人都可以請告訴我我在做什麼錯在這裏?我應該使用 不同的方法來執行此任務嗎?因爲它很難相信 你不可能在 一秒內有簡單的矩形平滑旋轉20次。
注意的是,雖然LunarLander示例演示流暢的2D動畫,它在其他方面非常馬車。 – 2011-02-04 22:11:39