Секундомер слишком медленный, в чём проблема?
У меня есть объект CountDownTimer, имеющий интервал в 10 миллисекунд. Я делал что-то вроде секундомера, который отображает значение миллисекунд в сотнях и десятках, то есть вместо 260 миллисекунд, он показывает 26, и по логике вещей каждые 10 миллисекунд значение должно было увеличиваться на 1: 27, 28, 29 и так далее. Но в итоге секундомер получился медленным, вот моё решение:
val timer = object : CountDownTimer(86400000, 10) {
override fun onTick(millisUntilFinished: Long) {
val h = findViewById<TextView>(R.id.hoursnumber)
val m = findViewById<TextView>(R.id.minutesnumber)
val s = findViewById<TextView>(R.id.secondsnumber)
val ms = findViewById<TextView>(R.id.milisecondsnumber)
if (indexMiliseconds == 99) {
if (indexSeconds == 59) {
if (indexMinutes == 59) {
indexHours++
indexMinutes = 0
indexMiliseconds = 0
} else {
indexMinutes++
indexSeconds = 0
indexMiliseconds = 0
}
} else {
indexSeconds++
indexMiliseconds = 0
}
}
indexMiliseconds += 1
ms.text = miliSeconds[indexMiliseconds]
s.text = seconds[indexSeconds]
m.text = minutes[indexMinutes]
h.text = hours[indexHours]
}
override fun onFinish() {
indexMinutes = 0
indexHours = 0
indexSeconds = 0
indexMiliseconds = 0
val h = findViewById<TextView>(R.id.hoursnumber)
val m = findViewById<TextView>(R.id.minutesnumber)
val s = findViewById<TextView>(R.id.secondsnumber)
val ms = findViewById<TextView>(R.id.milisecondsnumber)
ms.text = miliSeconds[indexMiliseconds]
s.text = seconds[indexSeconds]
m.text = minutes[indexMinutes]
h.text = hours[indexHours]
}
}.start()
private val hours: List<String> = listOf(
"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12",
"13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"
)
private var indexHours = 0
private val minutes: List<String> = listOf(
"00", "01", "02", "03", "04", "05", "06", "07", "08",
"09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22",
"23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36",
"37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49",
"50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60"
)
private var indexMinutes = 0
private val seconds: List<String> = listOf(
"00", "01", "02", "03", "04", "05", "06", "07", "08",
"09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22",
"23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36",
"37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49",
"50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60"
)
private var indexSeconds = 0
private val miliSeconds: List<String> = listOf(
"00",
"01",
"02",
"03",
"04",
"05",
"06",
"07",
"08",
"09",
"10",
"11",
"12",
"13",
"14",
"15",
"16",
"17",
"18",
"19",
"20",
"21",
"22",
"23",
"24",
"25",
"26",
"27",
"28",
"29",
"30",
"31",
"32",
"33",
"34",
"35",
"36",
"37",
"38",
"39",
"40",
"41",
"42",
"43",
"44",
"45",
"46",
"47",
"48",
"49",
"50",
"51",
"52",
"53",
"54",
"55",
"56",
"57",
"58",
"59",
"60",
"61",
"62",
"63",
"64",
"65",
"66",
"67",
"68",
"69",
"70",
"71",
"72",
"73",
"74",
"75",
"76",
"77",
"78",
"79",
"80",
"81",
"82",
"83",
"84",
"85",
"86",
"87",
"88",
"89",
"90",
"91",
"92",
"93",
"94",
"95",
"96",
"97",
"98",
"99",
"100"
)
private var indexMiliseconds = 0```