我试图执行一个基本的任务,旋转画布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