android修改当前窗口的亮度

获取系统当前亮度

/***
     * 获取系统亮度
     * @return
     */
    private int getSystemBrightness() {
        int systemBrightness = 0;
        try {
            systemBrightness = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
        } catch (Settings.SettingNotFoundException e) {
            e.printStackTrace();
        }
        return systemBrightness;
    }

修改当前窗口亮度

修改Window的screenBrightness属性实现,其中lp.screenBrightness的取值范围是0-1,在这个范围内取值越大亮度越高,小于0或者大于1时将使用系统当前亮度

/***
     * 调整屏幕亮度
     * @param brightness
     */
    public void changeAppBrightness(int brightness) {
        Window window = this.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        if (brightness == -1) {
            lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
        } else {
            lp.screenBrightness = (brightness <= 0 ? 1 : brightness) / 255f;
        }
//        lp.screenBrightness = 0.6f;
        window.setAttributes(lp);
    }

你可能感兴趣的:(Android)