【发布时间】:2015-12-19 21:34:22
【问题描述】:
我想在 android 中创建一个从 1 秒开始的 slow 秒表。除了一件事,一切都很好。我希望它是 slow 意思是:
1.000/s //Start Time
1.001/s //After 10ms
1.002/s //After another 10ms
这不是一个真正的秒表,但它有点像。对于我正在创建的游戏,我需要它来增加每 10 毫秒的时间。请在模拟器或真机上测试代码以便更好地理解。
代码:
private void startTime() {
final long startTime = SystemClock.uptimeMillis();
timeHandler = new Handler(); //Handler
timeRunnable = new Runnable() { //Runnable
@Override
public void run() {
long timeInMilliseconds = SystemClock.uptimeMillis() - startTime + 1000; //Start time is 1.000/s
int secs = (int) (timeInMilliseconds / 1000);
int mins = secs / 60;
secs = secs % 60;
int milliseconds = (int) (timeInMilliseconds % 1000);
if (timeInMilliseconds > 60000) {
score.setText(mins + ":" + String.format("%02d", secs) + "." + String.format("%03d", milliseconds) + "/s");
} else {
score.setText(String.format("%01d", secs) + "." + String.format("%03d", milliseconds) + "/s");
}
timeHandler.postDelayed(this, 0);
}
};
timeHandler.postDelayed(timeRunnable, 0);
}
注意 score 只是一个普通的 TextView 来显示时间。请帮我。谢谢。
【问题讨论】:
标签: android time runnable stopwatch android-handler