我正在尝试通过更新以下内容来修改屏幕亮度:
window.attributes = window.attributes.apply { screenBrightness = 0.2f }
但是我只能在 MainActivity : ComponentActivity() 中获得 window 。如何从可组合组件访问?
@Composable
fun BrightnessControl(
changeBrightness: (Float) -> Unit // Callback to change brightness
) {
val view = LocalView.current
val context = LocalView.current.context
Slider(
value = 0.2f, // Set your desired brightness value
onValueChange = { newValue ->
changeBrightness(newValue)
}
)
}
@Composable
fun MyScreen() {
var brightness by remember { mutableStateOf(0.5f) }
BrightnessControl { newBrightness ->
// Update the screen brightness
brightness = newBrightness
val activity = LocalView.current.context as? Activity
activity?.window?.attributes = activity.window.attributes.apply {
screenBrightness = newBrightness
}
}
// The rest of your Composable UI
}