requestedFrameRate
Common
Modifier in Compose Ui
Set a requested frame rate on Composable
You can set the preferred frame rate (frames per second) for a Composable using a positive number. This API should only be used when a specific frame rate is needed for your Composable.
Keep in mind that the preferred frame rate affects the frame rate for the next frame, so use this method carefully. It's important to note that the preference is valid as long as the Composable is drawn.
Last updated:
Installation
dependencies {
implementation("androidx.compose.ui:ui:1.9.0-alpha01")
}
Overloads
fun Modifier.requestedFrameRate(frameRate: Float)
Parameters
name | description |
---|---|
frameRate | The preferred frame rate the content should be rendered at. Default value is 0. |
fun Modifier.requestedFrameRate(frameRateCategory: FrameRateCategory)
Parameters
name | description |
---|---|
frameRateCategory | The preferred frame rate category the content should be rendered at. |
Code Examples
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.requestedFrameRate(30f)
)
}
}
}
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.requestedFrameRate(FrameRateCategory.High)
)
}
}
}