当前位置:   article > 正文

Android系统屏幕亮度调节Brightness

Android系统屏幕亮度调节Brightness

屏幕亮度Brightness

源码部分

设置-显示-亮度百分比

packages\apps\Settings\src\com\android\settings\display\BrightnessLevelPreferenceController.java

private void updatedSummary(Preference preference) {
    if (preference != null) {
        preference.setSummary(NumberFormat.getPercentInstance().format(getCurrentBrightness()));
    }
}

private double getCurrentBrightness() {
    final int value;
    if (isInVrMode()) {
        value = convertLinearToGamma(System.getInt(mContentResolver,
                System.SCREEN_BRIGHTNESS_FOR_VR, mMaxBrightness),
                mMinVrBrightness, mMaxVrBrightness);
    } else {
        value = convertLinearToGamma(Settings.System.getInt(mContentResolver,
                System.SCREEN_BRIGHTNESS, mMinBrightness),
                mMinBrightness, mMaxBrightness);
    }
    return getPercentage(value, 0, GAMMA_SPACE_MAX);
}

private double getPercentage(double value, int min, int max) {
    if (value > max) {
        return 1.0;
    }
    if (value < min) {
        return 0.0;
    }
    return (value - min) / (max - min);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

convertLinearToGamma()非线性计算转化

frameworks\base\packages\SettingsLib\src\com\android\settingslib\display\BrightnessUtils.java

public static final int convertLinearToGamma(int val, int min, int max) {
    // For some reason, HLG normalizes to the range [0, 12] rather than [0, 1]
    final float normalizedVal = MathUtils.norm(min, max, val) * 12;
    final float ret;
    if (normalizedVal <= 1f) {
        ret = MathUtils.sqrt(normalizedVal) * R;
    } else {
        ret = A * MathUtils.log(normalizedVal - B) + C;
    }

    return Math.round(MathUtils.lerp(0, GAMMA_SPACE_MAX, ret));
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

BrightnessDialog及其控制

# BrightnessDialog
frameworks\base\packages\SystemUI\src\com\android\systemui\settings\BrightnessDialog.java

final ImageView icon = findViewById(R.id.brightness_icon);
final ToggleSliderView slider = findViewById(R.id.brightness_slider);
mBrightnessController = new BrightnessController(this, icon, slider);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
# BrightnessDialog控制
frameworks\base\packages\SystemUI\src\com\android\systemui\settings\BrightnessController.java

@Override
public void onChanged(ToggleSlider toggleSlider, boolean tracking, boolean automatic,
        int value, boolean stopTracking) {
    updateIcon(mAutomatic);
    if (mExternalChange) return;

    if (mSliderAnimator != null) {
        mSliderAnimator.cancel();
    }

    final int min;
    final int max;
    final int metric;
    final String setting;

    if (mIsVrModeEnabled) {
        metric = MetricsEvent.ACTION_BRIGHTNESS_FOR_VR;
        min = mMinimumBacklightForVr;
        max = mMaximumBacklightForVr;
        setting = Settings.System.SCREEN_BRIGHTNESS_FOR_VR;
    } else {
        metric = mAutomatic
                ? MetricsEvent.ACTION_BRIGHTNESS_AUTO
                : MetricsEvent.ACTION_BRIGHTNESS;
        min = mMinimumBacklight;
        max = mMaximumBacklight;
        setting = Settings.System.SCREEN_BRIGHTNESS;
    }

    final int val = convertGammaToLinear(value, min, max);
    
    if (stopTracking) {
        MetricsLogger.action(mContext, metric, val);
    }

    setBrightness(val);
    if (!tracking) {
        AsyncTask.execute(new Runnable() {
                public void run() {
                    Settings.System.putIntForUser(mContext.getContentResolver(),
                            setting, val, UserHandle.USER_CURRENT);
                }
            });
    }

    for (BrightnessStateChangeCallback cb : mChangeCallbacks) {
        cb.onBrightnessLevelChanged();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
private void updateSlider(int val, boolean inVrMode) {
    final int min;
    final int max;
    if (inVrMode) {
        min = mMinimumBacklightForVr;
        max = mMaximumBacklightForVr;
    } else {
        min = mMinimumBacklight;
        max = mMaximumBacklight;
    }
    if (val == convertGammaToLinear(mControl.getValue(), min, max)) {
        // If we have more resolution on the slider than we do in the actual setting, then
        // multiple slider positions will map to the same setting value. Thus, if we see a
        // setting value here that maps to the current slider position, we don't bother to
        // calculate the new slider position since it may differ and look like a brightness
        // change to the user even though it isn't one.
        return;
    }
    final int sliderVal = convertLinearToGamma(val, min, max);
    animateSliderTo(sliderVal);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
亮度参数配置

默认亮度、自动亮度开关

frameworks\base\packages\SettingsProvider\res\values\defaults.xml

<!-- Default screen brightness, from 0 to 255.  102 is 40%. -->
<integer name="def_screen_brightness">102</integer>
<bool name="def_screen_brightness_automatic_mode">false</bool>
  • 1
  • 2
  • 3
  • 4
  • 5

最大亮度、最小亮度

frameworks\base\core\res\res\values\config.xml

<!-- Minimum screen brightness setting allowed by the power manager.
     The user is forbidden from setting the brightness below this level. -->
<integer name="config_screenBrightnessSettingMinimum">10</integer>

<!-- Maximum screen brightness allowed by the power manager.
     The user is forbidden from setting the brightness above this level. -->
<integer name="config_screenBrightnessSettingMaximum">255</integer>

<!-- Default screen brightness setting.
     Must be in the range specified by minimum and maximum. -->
<integer name="config_screenBrightnessSettingDefault">102</integer>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
adb命令控制亮度

查看亮度值

adb shell
$ cd /sys/class/leds/lcd-backlight
$ ls
$ brightness device max_brightness power subsystem trigger uevent
$ cat brightness 
  • 1
  • 2
  • 3
  • 4
  • 5
# 获取亮度是否为自动获取
C:\Users\Administrator>adb shell settings get system screen_brightness_mode
1
# 获取当前亮度值
C:\Users\Administrator>adb shell settings get system screen_brightness
30
# 更改亮度值(亮度值在0—255之间整数)
C:\Users\Administrator>adb shell settings put system screen_brightness 150
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/642986
推荐阅读
相关标签
  

闽ICP备14008679号