如何在 android 中创建慢速秒表?
How to create a slow stopwatch in android?
我想在 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);
}
请注意,分数只是一个显示时间的普通 TextView。请帮我。谢谢
经过反复试验,我找到了一个非常简单的解决方案,我将post放在这里,以防将来有人遇到像我一样的问题。
private void startTime() {
final long startTime = SystemClock.uptimeMillis();
timeHandler = new Handler();
timeRunnable = new Runnable() {
@Override
public void run() {
//Note the difference in the first two lines here.
long timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
//You can change this 20 according to what you want.
timeInMilliseconds = (timeInMilliseconds / 20) + 1000;
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(timeRunnable, 0);
}
};
timeHandler.postDelayed(timeRunnable, 0);
}
现在,秒表速度已经降低。 我把时间每 20ms 增加 1ms 而不是之前问题中提到的 10ms 因为这样更适合我。当然你可以把这个20改成任何你想要的。
我想在 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);
}
请注意,分数只是一个显示时间的普通 TextView。请帮我。谢谢
经过反复试验,我找到了一个非常简单的解决方案,我将post放在这里,以防将来有人遇到像我一样的问题。
private void startTime() {
final long startTime = SystemClock.uptimeMillis();
timeHandler = new Handler();
timeRunnable = new Runnable() {
@Override
public void run() {
//Note the difference in the first two lines here.
long timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
//You can change this 20 according to what you want.
timeInMilliseconds = (timeInMilliseconds / 20) + 1000;
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(timeRunnable, 0);
}
};
timeHandler.postDelayed(timeRunnable, 0);
}
现在,秒表速度已经降低。 我把时间每 20ms 增加 1ms 而不是之前问题中提到的 10ms 因为这样更适合我。当然你可以把这个20改成任何你想要的。