自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
实现护眼模式
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
研究表明,屏幕发出的蓝光会对睡眠质量造成不良影响。Android 7.1.1 推出了一种称为“护眼模式”的功能,可减少设备屏幕发出的蓝光量,以便达到更接近用户当前时间和地点的自然光效果。Android 8.0 推出一项附加功能,可让用户更好地控制护眼模式效果的强度。
Android 10 推出了 COLOR_DISPLAY_SERVICE
系统服务,具备系统 API surface,使系统、“设置”和系统界面能够更好地控制所有颜色转换(包括护眼模式)。
若要实现护眼模式,必须实现 Hardware Composer HAL 2.0 (HWC 2),以便根据传递到 setColorTransform
的矩阵进行色调调节,而不会影响耗电量、性能和应用兼容性。
实现
设备制造商可以使用在以下文件中定义的下列标记启用该功能的默认实现:frameworks/base/core/res/res/values/config.xml
<!-- Control whether Night display is available. This should only be enabled
on devices with HWC 2 color transform support. -->
<bool name="config_nightDisplayAvailable">false</bool>
<!-- Default mode to control how Night display is automatically activated.
One of the following values (see NightDisplayController.java):
0 - AUTO_MODE_DISABLED
1 - AUTO_MODE_CUSTOM
2 - AUTO_MODE_TWILIGHT
-->
<integer name="config_defaultNightDisplayAutoMode">0</integer>
<!-- Default time when Night display is automatically activated.
Represented as milliseconds from midnight (e.g. 79200000 == 10pm). -->
<integer name="config_defaultNightDisplayCustomStartTime">79200000</integer>
<!-- Default time when Night display is automatically deactivated.
Represented as milliseconds from midnight (e.g. 21600000 == 6am). -->
<integer name="config_defaultNightDisplayCustomEndTime">21600000</integer>
<!-- Minimum color temperature, in Kelvin, supported by Night display. -->
<integer name="config_nightDisplayColorTemperatureMin">2596</integer>
<!-- Default color temperature, in Kelvin, to tint the screen when Night display is
activated. -->
<integer name="config_nightDisplayColorTemperatureDefault">2850</integer>
<!-- Maximum color temperature, in Kelvin, supported by Night display. -->
<integer name="config_nightDisplayColorTemperatureMax">4082</integer>
这段代码包含用于框架、系统服务、系统界面和“设置”的代码。核心功能由 ColorDisplayManager
(由 ColorDisplayService
支持)控制。
设备制造商应根据设备显示面板的特性(包括白点、色域和所需颜色)自定义颜色梯度。您可以使用配置叠加更改颜色梯度,而不更改基本实现。此配置表示为红色、绿色和蓝色中每种颜色的二次方程,其形式为 vres = vat2 + vbt + vy-int,其中 t 是以开尔文为单位的温度输入,根据 config_nightDisplayColorTemperatureMin
和 config_nightDisplayColorTemperatureMax
之间的范围(如上一部分所述)指定,va、vb 和 vy-int 分别是指定主元素的曲线的 a 系数、b 系数和 y 轴截距,如下所示。
<string-array name="config_nightDisplayColorTemperatureCoefficientsNative">
<!-- R a-coefficient --> <item>0.0</item>
<!-- R b-coefficient --> <item>0.0</item>
<!-- R y-intercept --> <item>1.0</item>
<!-- G a-coefficient --> <item>-0.00000000962353339</item>
<!-- G b-coefficient --> <item>0.000153045476</item>
<!-- G y-intercept --> <item>0.390782778</item>
<!-- B a-coefficient --> <item>-0.0000000189359041</item>
<!-- B b-coefficient --> <item>0.000302412211</item>
<!-- B y-intercept --> <item>-0.198650895</item>
</string-array>
<string-array name="config_nightDisplayColorTemperatureCoefficients">
<!-- R a-coefficient --> <item>0.0</item>
<!-- R b-coefficient --> <item>0.0</item>
<!-- R y-intercept --> <item>1.0</item>
<!-- G a-coefficient --> <item>-0.00000000962353339</item>
<!-- G b-coefficient --> <item>0.000153045476</item>
<!-- G y-intercept --> <item>0.390782778</item>
<!-- B a-coefficient --> <item>-0.0000000189359041</item>
<!-- B b-coefficient --> <item>0.000302412211</item>
<!-- B y-intercept --> <item>-0.198650895</item>
</string-array>
界面功能
由于护眼模式是面向用户的功能,因此用户需要能够掌控这项功能。Android 开源项目 (AOSP) 的 packages/apps/Settings 项目中提供了全面的设置实现方法,以供设备制造商在实现其“设置”时参考。实现人员必须处理 Settings.ACTION_NIGHT_DISPLAY_SETTINGS
intent 才能显示此设置。
设置
用户可以在“设置”>“显示”>“护眼模式”中设置护眼模式。在相应页面中,用户可以了解护眼模式,设置其时间安排以及开启或关闭护眼模式。
- 自动开启
- 永不:护眼模式一律不自动开启,且必须通过手动切换开启/关闭开关进行启用。
- 自定义时间安排:护眼模式在指定的开始时间(默认为晚上 10:30)开启,并在指定的结束时间(默认为早上 6:30)关闭。
- 日落到日出:在日落时开启护眼模式,在日出时关闭护眼模式。日出和日落的时间取决于设备所处的位置和当地的时节。
- 开启/关闭:用于控制护眼模式当前状态的切换开关。此状态遵循现有的自动规则。例如,如果用户在下午 5:30 开启护眼模式(早于自动规则的开启时间,即晚上 10:30),系统依然会在早上 6:30 关闭护眼模式。如果用户在早上 5:30 关闭护眼模式(早于自动规则的关闭时间,即早上 6:30),系统依然会在晚上 10:30 开启护眼模式。
- 强度:通过从暖色调滑动到冷色调以控制着色程度的拖动条。在未启用护眼模式时,可以停用拖动条。
- 信息性文本:向用户介绍护眼模式的功能和原理。
设置条件语句
当护眼模式处于开启状态时,用户可以在“设置”顶部看到。
“快捷设置”功能块
“快捷设置”功能块的行为方式与“设置”>“显示”>“护眼模式”中的开启/关闭切换开关的行为方式相同。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Implement night light\n\nResearch suggests that blue light from screens can have a negative impact on\nsleep. Android 7.1.1 introduced a feature called Night Light that reduces the\namount of blue light emitted by the device display to better match the natural\nlight of the user's time of day and location. Android 8.0 introduced an additional\nfeature that gives users more control over the intensity of the Night Light effect.\nAndroid 10 introduces the\n`COLOR_DISPLAY_SERVICE` system service, with a system API\nsurface to give the system, Settings, and System UI more control over all color\ntransforms, including Night Light.\n\n\nNight Light requires a\n[Hardware\nComposer HAL 2.0](/docs/core/graphics/implement-hwc) (HWC 2) implementation that can apply the matrix passed to\n`setColorTransform` to perform tinting without impacting power,\nperformance, and app compatibility.\n\nImplementation\n--------------\n\n\nDevice manufacturers can enable the default implementation of the feature by\nusing the following flags defined in\n[frameworks/base/core/res/res/values/config.xml](https://android.googlesource.com/platform/frameworks/base/+/android16-release/core/res/res/values/config.xml)\n\n```\n \u003c!-- Control whether Night display is available. This should only be enabled\n on devices with HWC 2 color transform support. --\u003e\n \u003cbool name=\"config_nightDisplayAvailable\"\u003efalse\u003c/bool\u003e\n \u003c!-- Default mode to control how Night display is automatically activated.\n One of the following values (see NightDisplayController.java):\n 0 - AUTO_MODE_DISABLED\n 1 - AUTO_MODE_CUSTOM\n 2 - AUTO_MODE_TWILIGHT\n --\u003e\n \u003cinteger name=\"config_defaultNightDisplayAutoMode\"\u003e0\u003c/integer\u003e\n \u003c!-- Default time when Night display is automatically activated.\n Represented as milliseconds from midnight (e.g. 79200000 == 10pm). --\u003e\n \u003cinteger name=\"config_defaultNightDisplayCustomStartTime\"\u003e79200000\u003c/integer\u003e\n \u003c!-- Default time when Night display is automatically deactivated.\n Represented as milliseconds from midnight (e.g. 21600000 == 6am). --\u003e\n \u003cinteger name=\"config_defaultNightDisplayCustomEndTime\"\u003e21600000\u003c/integer\u003e\n\n \u003c!-- Minimum color temperature, in Kelvin, supported by Night display. --\u003e\n \u003cinteger name=\"config_nightDisplayColorTemperatureMin\"\u003e2596\u003c/integer\u003e\n \u003c!-- Default color temperature, in Kelvin, to tint the screen when Night display is\n activated. --\u003e\n \u003cinteger name=\"config_nightDisplayColorTemperatureDefault\"\u003e2850\u003c/integer\u003e\n \u003c!-- Maximum color temperature, in Kelvin, supported by Night display. --\u003e\n \u003cinteger name=\"config_nightDisplayColorTemperatureMax\"\u003e4082\u003c/integer\u003e\n```\n\n\nThe code is divided between framework, system services, System UI, and Settings. The\ncore functionality is controlled by `ColorDisplayManager` (backed by\n`ColorDisplayService`).\n\n\nDevice manufacturers should customize the color ramp based on the characteristics of the\ndevice's display panel, including white point, gamut, and desired color. You can change the\ncolor ramp without changing the base implementation by using a configuration overlay.\nThis configuration is expressed as a quadratic equation for each of red, green, and blue,\nin the form v~res~ = v~a~t^2^ + v~b~t + v~y-int~\nwhere t is the temperature input in Kelvin, as specified in the range between\n`config_nightDisplayColorTemperatureMin` and\n`config_nightDisplayColorTemperatureMax` (as described\nin the previous section), and v~a~, v~b~, and v~y-int~ are the\na-coefficient, b-coefficient, and y-intercept, respectively, for the given primary's curve,\nas indicated below. \n\n```\n \u003cstring-array name=\"config_nightDisplayColorTemperatureCoefficientsNative\"\u003e\n \u003c!-- R a-coefficient --\u003e \u003citem\u003e0.0\u003c/item\u003e\n \u003c!-- R b-coefficient --\u003e \u003citem\u003e0.0\u003c/item\u003e\n \u003c!-- R y-intercept --\u003e \u003citem\u003e1.0\u003c/item\u003e\n \u003c!-- G a-coefficient --\u003e \u003citem\u003e-0.00000000962353339\u003c/item\u003e\n \u003c!-- G b-coefficient --\u003e \u003citem\u003e0.000153045476\u003c/item\u003e\n \u003c!-- G y-intercept --\u003e \u003citem\u003e0.390782778\u003c/item\u003e\n \u003c!-- B a-coefficient --\u003e \u003citem\u003e-0.0000000189359041\u003c/item\u003e\n \u003c!-- B b-coefficient --\u003e \u003citem\u003e0.000302412211\u003c/item\u003e\n \u003c!-- B y-intercept --\u003e \u003citem\u003e-0.198650895\u003c/item\u003e\n \u003c/string-array\u003e\n\n \u003cstring-array name=\"config_nightDisplayColorTemperatureCoefficients\"\u003e\n \u003c!-- R a-coefficient --\u003e \u003citem\u003e0.0\u003c/item\u003e\n \u003c!-- R b-coefficient --\u003e \u003citem\u003e0.0\u003c/item\u003e\n \u003c!-- R y-intercept --\u003e \u003citem\u003e1.0\u003c/item\u003e\n \u003c!-- G a-coefficient --\u003e \u003citem\u003e-0.00000000962353339\u003c/item\u003e\n \u003c!-- G b-coefficient --\u003e \u003citem\u003e0.000153045476\u003c/item\u003e\n \u003c!-- G y-intercept --\u003e \u003citem\u003e0.390782778\u003c/item\u003e\n \u003c!-- B a-coefficient --\u003e \u003citem\u003e-0.0000000189359041\u003c/item\u003e\n \u003c!-- B b-coefficient --\u003e \u003citem\u003e0.000302412211\u003c/item\u003e\n \u003c!-- B y-intercept --\u003e \u003citem\u003e-0.198650895\u003c/item\u003e\n \u003c/string-array\u003e\n```\n\nUI features\n-----------\n\n\nBecause Night Light is a user-facing feature, users need to be able to control\nit. There is a full implementation of the settings in the Android Open Source\nProject (AOSP)\n[packages/apps/Settings](https://android.googlesource.com/platform/packages/apps/Settings/)\nproject that device manufacturers can reference for their Settings\nimplementation. Implementers must handle the\n[Settings.ACTION_NIGHT_DISPLAY_SETTINGS](https://developer.android.com/reference/android/provider/Settings.html#ACTION_NIGHT_DISPLAY_SETTINGS)\nintent to expose this setting.\n\n### Settings\n\n\nThe settings for Night Light are in *Settings \\\u003e Display \\\u003e Night\nLight*. From there, users can learn about Night Light, set its schedule,\nand turn it on or off.\n\n- **Turn On Automatically**\n - **Never:** Night Light will never turn on automatically and must be activated with the manual **On / Off** toggle.\n - **Custom schedule:** Night Light turns on at a specified **Start time** \\[default: 10:30 p.m.\\] and off at a specified **End time** \\[default: 6:30 a.m.\\].\n - **Sunset to sunrise:** Night Light turns on at sunset and off at sunrise. The time for sunrise and sunset depends on the device location and the time of year.\n- **On / Off:** Toggle that controls the current state of Night Light. This state respects existing automatic rules. For example, if Night Light is toggled on at 5:30 p.m. (before the automatic rule would turn it on at 10:30 p.m.), Night Light will still turn off at 6:30 a.m. And if Night Light is toggled off at 5:30 a.m. (before it turns off at 6:30 a.m.), it will still turn on at 10:30 p.m.\n- **Intensity:** [Seekbar](https://developer.android.com/reference/android/widget/SeekBar.html) that controls the tint level by sliding from warm to cool. The seekbar can be disabled when Night Light is not activated.\n- **Informational text:** Teaches the user what Night Light does and why.\n\n### Settings conditional\n\n\nVisible at the top of Settings when Night Light is on.\n\n### Quick Settings tile\n\n\nThe Quick Settings tile behaves identically to the **On / Off**\ntoggle in *Settings \\\u003e Display \\\u003e Night Light*."]]