SetFrameRateCategorySample
@Composable
fun SetFrameRateCategorySample() {
var targetAlpha by remember { mutableFloatStateOf(1f) }
val context = LocalContext.current
val activity: Activity? = findOwner(context)
DisposableEffect(activity) {
activity?.window?.frameRateBoostOnTouchEnabled = false
onDispose { activity?.window?.frameRateBoostOnTouchEnabled = true }
}
val alpha by
animateFloatAsState(targetValue = targetAlpha, animationSpec = tween(durationMillis = 5000))
Column(modifier = Modifier.size(300.dp)) {
Button(
onClick = { targetAlpha = if (targetAlpha == 1f) 0.2f else 1f },
modifier =
Modifier.testTag("frameRateTag")
.background(LocalContentColor.current.copy(alpha = alpha)),
) {
Text(
text = "Click Me for alpha change with frame rate category High",
color = LocalContentColor.current.copy(alpha = alpha),
modifier = Modifier.preferredFrameRate(FrameRateCategory.High),
)
}
}
}
SetFrameRateSample
@Composable
fun SetFrameRateSample() {
var targetAlpha by remember { mutableFloatStateOf(1f) }
val context = LocalContext.current
val activity: Activity? = findOwner(context)
DisposableEffect(activity) {
activity?.window?.frameRateBoostOnTouchEnabled = false
onDispose { activity?.window?.frameRateBoostOnTouchEnabled = true }
}
val alpha by
animateFloatAsState(targetValue = targetAlpha, animationSpec = tween(durationMillis = 5000))
Column(modifier = Modifier.size(300.dp)) {
Button(
onClick = { targetAlpha = if (targetAlpha == 1f) 0.2f else 1f },
modifier =
Modifier.testTag("frameRateTag")
.background(LocalContentColor.current.copy(alpha = alpha)),
) {
Text(
text = "Click Me for alpha change with 30 Hz frame rate",
color = LocalContentColor.current.copy(alpha = alpha), // Adjust text alpha
modifier = Modifier.preferredFrameRate(30f),
)
}
}
}