Created
May 5, 2023 11:39
-
-
Save PhilipDukhov/c4e6bb78376b25b273b3b794ffd8a8c5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Composable | |
fun FpsCounter( | |
modifier: Modifier = Modifier, | |
) { | |
var fps by remember { | |
mutableStateOf("") | |
} | |
LaunchedEffect(Unit) { | |
val frames = mutableListOf<Long>() | |
while (true) { | |
val frame = awaitFrame() | |
frames.add(frame) | |
frames.removeIf { frame - it > 1_000_000_000 } | |
if (frames.count() > 1) { | |
fps = (frames.count() / ((frames.last() - frames.first()).toFloat() / 1_000_000_000f)).roundToInt() | |
.toString() | |
} | |
} | |
} | |
Text( | |
fps, | |
color = Color.Black, | |
modifier = modifier | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment