Android 屏幕亮度调节

屏幕亮度调节模式:

Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC:值为1,自动调节亮度。

Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL:值为0,手动模式。

设置屏幕亮度调节模式为手动模式


    ContentResolver contentResolver = getActivity().getContentResolver();

    try {

        int mode = Settings.System.getInt(contentResolver,

                Settings.System.SCREEN_BRIGHTNESS_MODE);

        if (mode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {

            Settings.System.putInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS_MODE,

                    Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);

        }

    } catch (Settings.SettingNotFoundException e) {

        e.printStackTrace();
    }
} 

获取屏幕亮度值

  • 屏幕最大亮度为255。
  • 屏幕最低亮度为0。
  • 屏幕亮度值范围必须位于:0~255

设置屏幕亮度

private int getScreenBrightness() {
    ContentResolver contentResolver = getActivity().getContentResolver();
    int defVal = 125;
    return Settings.System.getInt(contentResolver,
            Settings.System.SCREEN_BRIGHTNESS, defVal);
}

设置系统亮度权限申明

设置当前窗口亮度

很多视频应用,在touch事件处理屏幕亮度时,并不是修改的系统亮度值,而是修改当前应用所在窗口的亮度。具体做法就是修改LayoutParams中的screenBrightness属性。参考代码如下:

private void setWindowBrightness(int brightness) {
    Window window = getWindow();
    WindowManager.LayoutParams lp = window.getAttributes();
    lp.screenBrightness = brightness / 255.0f;
    window.setAttributes(lp);
}

你可能感兴趣的:(Android 屏幕亮度调节)