当前位置:   article > 正文

Android屏幕亮度调节_android调节屏幕亮度

android调节屏幕亮度

本 APP 亮度设置

即仅在本 APP 处于前台时使用该屏幕亮度,一旦退出或者挂起 APP,屏幕亮度恢复原值

无需申请任何权限,拿来就用

获取当前窗口属性,设置属性的屏幕亮度值,然后在应用一下就好了!

亮度值 brightness0.0-1.0 之间

fun setAppBrightness(window: Window, brightness: Float) {
    val attributes = window.attributes
    attributes.screenBrightness = brightness
    window.attributes = attributes
}
  • 1
  • 2
  • 3
  • 4
  • 5

屏幕亮度相关 API

获取当前系统屏幕亮度

亮度值为浮点数,范围 0.0f-255.0f

fun getCurrentBrightness(context: Context): Float {
    var resolver = context.contentResolver
    var brightness = 0.0f
    try {
        brightness = Settings.System.getFloat(resolver, Settings.System.SCREEN_BRIGHTNESS)
    } catch (e: SettingNotFoundException) {
        e.printStackTrace()
    }
    return brightness
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

获取亮度模式

分别为:自动调节(数值 1),手动调节(数值 0)

fun getBrightnessMode(context: Context): Int {
    var mode: Int = -1
    try {
        mode = Settings.System.getInt(
            context.contentResolver,
            Settings.System.SCREEN_BRIGHTNESS_MODE,
            -1
        )
    } catch (e: SettingNotFoundException) {
        e.printStackTrace()
    }
    return mode
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

手机屏幕亮度设置

一旦设置,即更改手机全局屏幕亮度

先申请权限:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/643007
推荐阅读
相关标签
  

闽ICP备14008679号