Jetpack Compose 中的活动 window.attributes

问题描述 投票:0回答:1

我正在尝试通过更新以下内容来修改屏幕亮度:

   window.attributes = window.attributes.apply { screenBrightness = 0.2f }

但是我只能在 MainActivity : ComponentActivity() 中获得 window 。如何从可组合组件访问?

android android-jetpack-compose android-jetpack
1个回答
0
投票
@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
}
© www.soinside.com 2019 - 2024. All rights reserved.