Compose Modifier

preferredFrameRate

Set a requested frame rate on Composable You can set the preferred frame rate (frames per second) for a Composable using a non-negative number.

SetFrameRateSample

@Sampled
@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),
            )
        }
    }
}

SetFrameRateCategorySample

@Sampled
@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),
            )
        }
    }
}
Jetpack Compose beyond MaterialFree components, screens and templates for apps that don’t look like Google’s. Copy them into your app, or let your coding agent add them.Browse for free

Content updated: