黑莓秒表实现
我正在尝试编写一个黑莓应用程序,它基本上是一个秒表,并显示单圈时间。首先,我不确定我是否以最佳方式实现秒表功能。我有一个显示“时钟”的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里有一些提示:
在制作快速 UI 时要记住的最重要的事情是仅在以下情况下更新 UI您需要,并且仅更新需要更改的字段。如果你调用像deleteAll()这样的方法,你最终会刷新整个屏幕或管理器,这真的很慢。
Here are a few tips:
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.