Android activity 亮度调整

注意点 screenBrightness 取值范围0-1 不是0-255一定要注意

scanForActivity(context) 是根据上下文获取所在的activity
如果直接在activity 调用的话使用 this.window.attributes

/**
     * 获取当前屏幕亮度
     */
    fun getBrightness():Int {
        val lp = scanForActivity(context)?.window?.attributes
        if(lp != null){
            //screenBrightness 默认为-1
            if(lp.screenBrightness < 0) return 0
            return (lp.screenBrightness * 255).toInt()
        }
        return 0
    }

    /**
     * 设置屏幕亮度
     * @param brightness 取值范围 0-255
     */
    fun setBrightness(brightness:Int){
        val lp = scanForActivity(context)?.window?.attributes
        if(lp != null){
            lp.screenBrightness = brightness.toFloat() / 255
            ChaoUtil.scanForActivity(context)?.window?.attributes = lp
        }
    }
原文地址:https://www.cnblogs.com/rchao/p/11233008.html