黑莓秒表实现

发布于 2024-08-24 07:43:10 字数 1065 浏览 6 评论 0原文

我正在尝试编写一个黑莓应用程序,它基本上是一个秒表,并显示单圈时间。首先,我不确定我是否以最佳方式实现秒表功能。我有一个显示“时钟”的 LabelField (_myLabel) - 从 00:00 开始。然后您点击开始按钮,每秒 _myLabel 字段都会更新自上次更新以来经过的秒数(应该只增加 1,但有时会有延迟,它会跳过一个数字)。我只是想不出不同的方法来做到这一点 - 而且我是 GUI 开发和线程的新手,所以我想这就是原因。

编辑:这就是所谓的秒表:

_timer = new Timer();
_timer.schedule(new MyTimerTask(), 250, 250);  

这是计时器任务:

class MyTimerTask extends TimerTask {
long currentTime;
long startTime = System.currentTimeMillis();

public void run() {
synchronized (Application.getEventLock()) {
currentTime = System.currentTimeMillis();
long diff = currentTime - startTime;
long min = diff / 60000;
long sec = (diff % 60000) / 1000;
String minStr = new Long(min).toString();
String secStr = new Long(sec).toString();

if (min < 10)
 minStr = "0" + minStr;
if (sec < 10)
 secStr = "0" + secStr;

 _myLabel.setText(minStr + ":" + secStr);  
timerDisplay.deleteAll();
timerDisplay.add(_timerLabel);
 }
 }
}

无论如何,当您停止秒表时,它会更新单圈时间数据的历史表。当这个列表变长时,计时器开始退化。如果你尝试滚动,那么情况会变得非常糟糕。

有没有更好的方法来实现我的秒表?

I'm trying to write a blackberry app that is basically a stopwatch, and displays lap times. First, I'm not sure I'm implementing the stopwatch functionality in the most optimal way. I have a LabelField (_myLabel) that displays the 'clock' - starting at 00:00. Then you hit the start button and every second the _myLabel field gets updated with how many seconds have past since the last update (should only ever increment by 1, but sometimes there is a delay and it will skip a number). I just can't think of a different way to do it - and I am new to GUI development and threads so I guess that's why.

EDIT: Here is what calls the stopwatch:

_timer = new Timer();
_timer.schedule(new MyTimerTask(), 250, 250);  

And here is the TimerTask:

class MyTimerTask extends TimerTask {
long currentTime;
long startTime = System.currentTimeMillis();

public void run() {
synchronized (Application.getEventLock()) {
currentTime = System.currentTimeMillis();
long diff = currentTime - startTime;
long min = diff / 60000;
long sec = (diff % 60000) / 1000;
String minStr = new Long(min).toString();
String secStr = new Long(sec).toString();

if (min < 10)
 minStr = "0" + minStr;
if (sec < 10)
 secStr = "0" + secStr;

 _myLabel.setText(minStr + ":" + secStr);  
timerDisplay.deleteAll();
timerDisplay.add(_timerLabel);
 }
 }
}

Anyway when you stop the stopwatch it updates a historical table of lap time data. When this list gets long, the timer starts to degrade. If you try to scroll, then it gets really bad.

Is there a better way to implement my stopwatch?

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

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

发布评论

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

评论(1

眼泪都笑了 2024-08-31 07:43:10

这里有一些提示:

  • 跟踪用于更新标签的最后一个“秒”值,如果新计算的“秒”值相同,则立即退出运行循环 - 否则您将不必要地刷新 UI减慢一切速度的相同值
  • 删除运行循环中的同步,只需将修改 UI(setText 调用)的代码放入 UiApplication.getUiApplication.invokeLater() 调用(使用匿名 Runnable)中,
  • 不要删除重新 -从屏幕或 maanger 添加标签,您只需要调用 setText() 并且它应该更新 - 如果它没有更新,则在字段上调用 ​​invalidate() ,
  • 现在您已经优化了代码并且 它将被重新绘制最大限度地减少实际 UI 绘制量,可以安全地将 timertask 间隔设置为较低的值,例如 50ms,以便您拥有更平滑的计时器更新

在制作快速 UI 时要记住的最重要的事情是仅在以下情况下更新 UI您需要,并且仅更新需要更改的字段。如果你调用像deleteAll()这样的方法,你最终会刷新整个屏幕或管理器,这真的很慢。

Here are a few tips:

  • keep track of the last "sec" value used to update the label, and exit from the run loop immediately if the newly-calculated "sec" value is the same - otherwise you're needlessly refreshing the UI with the same values which slows everything down
  • remove the synchronization in your run loop and just put the code that modifies the UI (setText call) in a UiApplication.getUiApplication.invokeLater() call (using an anonymous Runnable)
  • don't delete the re-add the label from the screen or maanger, you just need to call setText() and it should update - if it doesn't update then call invalidate() on the field and it will be redrawn
  • now that you've optimized your code and minimized the amount of actual UI drawing, it's safe to set the timertask interval to a lower value, such as 50ms, so that you have a smoother timer update

The most important thing to remember in making a fast UI is to only update the UI when you need to, and only update the fields that need to change. If you're calling methods like deleteAll() you're going to end up having the entire screen or manager refresh which is really really slow.

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