优化刷新率
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Android 15 为游戏设置了默认的 60Hz 刷新率,以优化功耗。现在,如需解锁更高的帧速率(例如 120 FPS),您必须使用 Frame Rate API 或 Swappy 库明确请求这些帧速率。
不过,系统可能会根据电池电量或设备温度等因素替换此请求。虽然刷新率越高,视觉效果越流畅,但也需要消耗更多电量并产生更多热量。因此,请务必为用户提供选择首选刷新率的选项,并仔细监控性能,以确保提供平衡的用户体验。
借助 setFrameRate()
API,游戏开发者可以使用特定的显示刷新率。此操作分为以下两个步骤:
- 验证设备和 Android 版本的兼容性。
- 使用
setFrameRate()
请求高 FPS。
验证设备和 Android 版本的兼容性:
使用方法 Display.getSupportedModes()
确定设备是否支持 90Hz、120Hz 或其他刷新率。如果设备限制为 60Hz,则无法超出此限制。
Kotlin
val display = windowManager.defaultDisplay
val supportedModes = display.supportedModes
for (mode in supportedModes) {
Log.d("DisplayInfo", "Supported mode: ${mode.physicalWidth}x${mode.physicalHeight}, ${mode.refreshRate}Hz")
}
请求高 FPS
在渲染循环开始时、游戏窗口初始化期间或目标 FPS 需要更改显示刷新率时,调用 setFrameRate()
。
即使您请求更高的刷新率,系统可能仍会因省电模式或温控降频等因素而将刷新率限制为 60Hz。如果游戏的渲染性能未达到目标 FPS,请求更高的刷新率可能会导致不必要的功耗增加,并使设备温度升高。
以下代码段演示了如何使用 setFrameRate()
API 避免刷新率过高。
Kotlin
val targetFps = 120f
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
window.setFrameRate(
targetFps,
Window.FrameRateCompatibility.FRAME_RATE_COMPATIBILITY_FIXED_SOURCE,
0
)
}
如需进一步了解,请参阅“帧速率”页面。
使用帧同步库
帧同步库(也称为 Swappy)是一个开源库,旨在简化 C/C++ Android 游戏引擎中的 VSync 管理和帧调度。此工具可简化优化刷新率的过程,并有效地充当 setFrameRate()
等功能的更高级别抽象层。此外,Swappy 还提供其他功能,可提升游戏的整体流畅度和性能。
如需了解更多详情,请参阅 Swappy 页面。
其他提示(以便取得最佳效果)
以下部分列出了几条重要提示:
- 动态帧速率切换。
- 性能监控。
- 根据最大显示刷新率提供 FPS 选项。
动态帧速率切换
如需同时优化性能和功耗,请考虑在游戏中实现动态帧速率切换。借助此技术,您可以在更高的刷新率(例如 120Hz)之间无缝切换,以便在要求苛刻的场景中获得更流畅的游戏体验,并在低强度时刻或电池续航时间受到关注或以低于 60FPS 的帧速率为目标时,切换到较低的刷新率(例如 60Hz)。持续以 120Hz 运行可能会导致过度发热和电池快速耗尽,从而可能导致用户体验不佳。通过根据当前的渲染负载和设备条件智能调整刷新率,您可以在视觉保真度和功耗效率之间取得平衡。
为确保您的游戏在更高的刷新率下获得最佳性能,请集成帧计数器或性能叠加层等性能监控工具。这些工具会针对游戏的实际帧速率提供实时反馈,以便您验证是否始终能达到目标 120 FPS。
如果帧速率出现明显波动,请考虑在给定设备上以较低的可实现帧速率为目标。这样可以提供更流畅的体验,而不会出现在追求最高刷新率时可能会出现的性能故障。
根据最大显示屏刷新率提供 FPS 选项
您的游戏应检测当前设备支持的最大显示刷新率(例如 60Hz、90Hz 或 120Hz),并相应地限制 FPS 设置。例如,如果设备最高仅支持 60Hz,建议在游戏设置中停用高于 60FPS 的所有选项,以免让玩家感到困惑。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Optimize refresh rates\n\n[Android 15](/about/versions/15) sets a default 60Hz refresh rate for games to optimize\npower consumption. To unlock higher frame rates like 120 FPS, you must now\nexplicitly request them using the [Frame Rate API](/media/optimize/performance/frame-rate) or the Swappy\nlibrary.\n\nHowever, the system may override this request based on factors like battery\nlevel or device temperature. While higher refresh rates enhance visual\nsmoothness, they also demand more power and generate additional heat. Therefore,\nit's crucial to offer users the option to choose their preferred refresh rate\nand carefully monitor performance to ensure a balanced user experience.\n\nUse the [`setFrameRate()`](/reference/android/view/Surface#setFrameRate(float,%20int)) API\n------------------------------------------------------------------------------------------\n\nThe [`setFrameRate()`](/reference/android/view/Surface#setFrameRate(float,%20int)) API allows the game devs to use a specific display\nrefresh rate. There are two steps to doing this:\n\n1. Verify device and Android version compatibility.\n2. Request high FPS using [`setFrameRate()`](/reference/android/view/Surface#setFrameRate(float,%20int)).\n\n### Verify Device and Android Version Compatibility:\n\nUse methods [`Display.getSupportedModes()`](/reference/android/view/Display#getSupportedModes()) to determine if the device\nsupports 90Hz, 120Hz, or other refresh rates. If the device is limited to 60Hz,\nit is not possible to exceed this limit. \n\n### Kotlin\n\n val display = windowManager.defaultDisplay\n val supportedModes = display.supportedModes\n for (mode in supportedModes) {\n Log.d(\"DisplayInfo\", \"Supported mode: ${mode.physicalWidth}x${mode.physicalHeight}, ${mode.refreshRate}Hz\")\n }\n\n### Request High FPS\n\nInvoke [`setFrameRate()`](/reference/android/view/Surface#setFrameRate(float,%20int)) when your rendering loop starts, during game window\ninitialization, or when the target FPS needs to change the display refresh rate.\n\nEven if you request a higher rate, the system might still limit the refresh rate\nto 60Hz due to factors like power saving mode or thermal throttling. If your\ngame's rendering performance doesn't reach the target FPS, requesting higher\nrefresh rate may consume unnecessary power consumption and increase the device's\ntemperature.\n\nThe following snippet demonstrates how to avoid an overly high refresh rate with\nthe `setFrameRate()` API. \n\n### Kotlin\n\n val targetFps = 120f\n if (Build.VERSION.SDK_INT \u003e= Build.VERSION_CODES.R) {\n window.setFrameRate(\n targetFps,\n Window.FrameRateCompatibility.FRAME_RATE_COMPATIBILITY_FIXED_SOURCE,\n 0\n )\n }\n\n[The Framerate page](/media/optimize/performance/frame-rate) provides more detailed information for further reading.\n\nUse Frame pacing library\n------------------------\n\n[The frame pacing library](/media/optimize/performance/frame-rate), or Swappy, is an open-source library designed to\nsimplify [VSync](https://en.wikipedia.org/wiki/Vertical_blanking_interval) management and frame scheduling in C/C++ Android game\nengines.\nThis tool streamlines the process of optimizing refresh rates, effectively\nacting as a higher-level abstraction layer over functionalities like\n[`setFrameRate()`](/reference/android/view/Surface#setFrameRate(float,%20int)). Furthermore, Swappy provides additional features that can\nenhance your game's overall smoothness and performance.\n\n[The Swappy page](/media/optimize/performance/frame-rate) gives more detailed information.\n\nAdditional Tips for Best Results\n--------------------------------\n\nThe following section lay out several top tips:\n\n1. Dynamic Frame Rate Switching.\n2. Performance Monitoring.\n3. Provide FPS options based on maximum display refresh rate.\n\n### Dynamic Frame Rate Switching\n\nTo optimize both performance and power consumption, consider implementing\ndynamic frame rate switching in your game. This technique lets you\nseamlessly transition between higher refresh rates like 120Hz for smoother\ngameplay during demanding scenes, and lower rates like 60Hz during less\nintensive moments or when battery life is a concern or targeting under 60FPS.\nConstantly running at 120Hz can lead to excessive\n[heat generation](/games/optimize/adpf/thermal) and rapid battery drain, potentially resulting in a\nnegative user experience. By intelligently adjusting the refresh rate based on\nthe current rendering load and device conditions, you can strike a balance\nbetween visual fidelity and [power efficiency](/games/optimize/power).\n\n### Performance Monitoring\n\nTo ensure your game performs optimally at higher refresh rates, integrate\nperformance monitoring tools such as a frame counter or performance overlay.\nThese tools provide real-time feedback on your game's actual frame rate,\nallowing you to verify whether you're consistently achieving the target 120\nFPS.\n\nIf your frame rate fluctuates significantly, consider targeting a lower\nachievable framerate on the given device. This can deliver a smoother experience\nwithout the performance hiccups that might occur when striving for the highest\nrefresh rate.\n\n### Provide FPS options based on maximum display refresh rate\n\nYour game should detect the maximum display refresh rate supported by the\ncurrent device, such as 60Hz, 90Hz, or 120Hz, and limit the FPS settings\naccordingly. For example, if the device only supports up to 60Hz, it is\nadvisable to disable any options higher than 60FPS in the game settings to\navoid confusing the player."]]