【发布时间】:2011-06-21 15:46:12
【问题描述】:
我正在尝试执行将画布旋转 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 次 一秒钟。
【问题讨论】:
标签: android timer scheduled-tasks rotation