定时器任务未准确执行

发布于 2024-10-15 17:17:39 字数 1222 浏览 6 评论 0原文

我正在尝试执行每年旋转画布 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 次 一秒钟。

I am trying to perform basic task of rotating a canvas 20 times a
second using timer but it doesn't seem to be working properly and its
lagging. for example, if I rotate rectangle 0.3 degrees per 50 ms it
should rotate 6 degree in on second, but that is not the case. It
really slow in rotation. Here is my sample code:

//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);

Can anyone please tell me what am I doing wrong here? Should I used
different approach to perform this task? Because its hard to believe
that you cannot have simple smooth rotation of rectangle 20 times in
one second.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

庆幸我还是我 2024-10-22 17:17:39

Timer/TimerTask 不应该是精确的,也不应该用于此目的。请遵循 2D 游戏开发秘诀,例如 SDK 中包含的 LunarLander 示例。或者,您只需搜索 StackOverflow 并查找所有各种 有用的帖子 主题

Timer/TimerTask are not supposed to be precise and are not supposed to be used for this purpose. Follow the recipes for 2D game development, such as the LunarLander sample that is included in your SDK. Or, you could just search StackOverflow and find all sorts of useful posts on the subject.

缱绻入梦 2024-10-22 17:17:39

我相信你没有使用 SurfaceView。

像这样在画布上绘图是为了控制而不是快速渲染(阅读> 10fps)

如果你想要性能,你需要使用平均 25-30 fps 的 SurfaceView 或 opengl

请阅读:http://developer.android.com/guide/topics/graphics/index.html

I believe you are not using a SurfaceView.

Drawing like that to a canvas was meant for controls and not fast rendering(read >10fps)

If you want performance you need either to use a SurfaceView where you'll average a 25-30 fps or opengl

Please read : http://developer.android.com/guide/topics/graphics/index.html

感情洁癖 2024-10-22 17:17:39

调用 invalidate 的次数不必等于调用 onDraw 的次数。如果您的计时器在 UI 线程有机会运行之前连续运行两次,那么双重无效最终只会导致一次旋转。考虑放入调试代码来验证对这两个方法的调用次数。

The number of calls to invalidate need not equal the number of calls to onDraw. If your timer ends up running twice successively before the UI thread gets a chance to run, then the double invalidate will end up only causing a single rotation. Consider putting in debug code to validate the number of calls to those two methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文