性能等级是 Android 12 中首次引入的标准。性能等级定义了一组超出 Android 基准要求的设备功能。
每个 Android 版本都有其对应的性能等级,该等级定义在该版本的 Android 兼容性定义文档 (CDD) 中。由 Android 兼容性测试套件 (CTS) 验证 CDD 要求。
每台 Android 设备都声明其支持的性能等级。开发者可以在运行时找到设备的性能等级,并提供可充分利用设备功能的升级体验。
如需查找设备的性能等级,请使用 Jetpack 核心性能库。该库会报告设备在 构建版本信息中声明的媒体性能等级 (MPC) 或根据 Google Play 服务数据得出的媒体性能等级。
首先在您的 Gradle 文件中添加相关模块的依赖项
Kotlin
// Implementation of Jetpack Core library. implementation("androidx.core:core-ktx:1.12.0") // Enable APIs to query for device-reported performance class. implementation("androidx.core:core-performance:1.0.0") // Enable APIs to query Google Play services for performance class. implementation("androidx.core:core-performance-play-services:1.0.0")
Groovy
// Implementation of Jetpack Core library. implementation 'androidx.core:core-ktx:1.12.0' // Enable APIs to query for device-reported performance class. implementation 'androidx.core:core-performance:1.0.0' // Enable APIs to query Google Play services for performance class. implementation 'androidx.core:core-performance-play-services:1.0.0'
然后,在您的 Application
的 onCreate()
生命周期事件中,创建 DevicePerformance
实现(例如 PlayServicesDevicePerformance
)的实例。这在您的应用中只需执行一次。
Kotlin
import androidx.core.performance.play.services.PlayServicesDevicePerformance class MyApplication : Application() { lateinit var devicePerformance: DevicePerformance override fun onCreate() { // Use a class derived from the DevicePerformance interface devicePerformance = PlayServicesDevicePerformance(applicationContext) } }
Java
import androidx.core.performance.play.services.PlayServicesDevicePerformance; class MyApplication extends Application { DevicePerformance devicePerformance; @Override public void onCreate() { // Use a class derived from the DevicePerformance interface devicePerformance = new PlayServicesDevicePerformance(applicationContext); } }
然后,您可以检索 mediaPerformanceClass
属性,以根据设备的功能自定义您的应用体验
Kotlin
class MyActivity : Activity() { private lateinit var devicePerformance: DevicePerformance override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // Note: Good app architecture is to use a dependency framework. See // https://developer.android.com/training/dependency-injection for more // information. devicePerformance = (application as MyApplication).devicePerformance } override fun onResume() { super.onResume() when { devicePerformance.mediaPerformanceClass >= Build.VERSION_CODES.VANILLA_ICE_CREAM -> { // MPC level 35 and later. // Provide the most premium experience for the highest performing devices. } devicePerformance.mediaPerformanceClass == Build.VERSION_CODES.UPSIDE_DOWN_CAKE -> { // MPC level 34. // Provide a high quality experience. } else -> { // MPC level 33, 31, 30, or undefined. // Remove extras to keep experience functional. } } } }
Java
class MyActivity extends Activity { private DevicePerformance devicePerformance; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Note: Good app architecture is to use a dependency framework. See // https://developer.android.com/training/dependency-injection for more // information. devicePerformance = ((MyApplication) getApplication()).devicePerformance; } @Override public void onResume() { super.onResume(); if (devicePerformance.getMediaPerformanceClass() >= Build.VERSION_CODES.VANILLA_ICE_CREAM) { // MPC level 35 and later. // Provide the most premium experience for the highest performing devices. } else if (devicePerformance.getMediaPerformanceClass() == Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { // MPC level 34. // Provide a high quality experience. } else { // MPC level 33, 31, 30, or undefined. // Remove extras to keep experience functional. } } }
性能等级是向前兼容的。设备可以升级到更新的平台版本,而无需更新其性能等级。例如,如果设备最初支持性能等级 33,则可以升级到 Android 14,并且如果其不符合性能等级 34 的要求,则会继续报告支持性能等级 33。这样就可以对设备进行分组,而无需依赖特定的 Android 版本。
媒体性能等级 35
MPC 35 在 Android 15 中引入,基于 MPC 34 中引入的要求。具体的 MPC 35 要求发布在 Android 15 CDD 中。除了对 MPC 34 中的项目增加了要求外,CDD 还指定了以下领域的要求
媒体
- 解码丢帧
- HDR 编辑
- 动态色彩方面
- 纵向宽高比
相机
- JPEG_R
- 预览防抖
图形
- EGL 扩展程序
- Vulkan 结构
媒体性能等级 34
MPC 34 在 Android 14 中引入,基于 MPC 33 中引入的要求。具体的 MPC 34 要求发布在 Android 14 CDD 中。除了对 MPC 33 中的项目增加了要求外,CDD 还指定了以下领域的要求
媒体
- AV1 硬件解码器中的胶片颗粒效果支持
- AVIF 基线配置文件
- AV1 编码器性能
- HDR 视频编解码器
- RGBA_1010102 颜色格式
- YUV 纹理采样
- 视频编码质量
- 多声道音频混合
相机
- 夜间模式扩展
- 支持 HDR 的主摄像头
- 人脸检测场景模式
常规
- 硬件叠加层
- HDR 显示屏
媒体性能等级 33
MPC 33 在 Android 13 中引入,基于 MPC 31 中引入的要求。具体的 MPC 33 要求发布在 Android 13 CDD 中。除了对 MPC 31 中的项目增加了要求外,CDD 还指定了以下领域的要求
媒体
- AV1 硬件解码器
- 安全硬件解码器
- 解码器初始化延迟
- 往返音频延迟
- 有线耳机和 USB 音频设备
- MIDI 设备
- 硬件支持的信任执行环境
相机
- 预览防抖
- 慢动作录制
- 超广角摄像头的最小变焦倍数
- 并发摄像头
- 逻辑多摄像头
- 流式传输用例
媒体性能等级 31
MPC 31 在 Android 12 中引入。具体的 MPC 31 要求发布在 Android 12 CDD 中。CDD 指定了以下领域的要求
媒体
- 并发视频编解码会话
- 编码器初始化延迟
- 解码器丢帧
- 编码质量
相机
- 分辨率和帧速率
- 启动和捕获延迟
FULL
或更高级别的硬件- 时间戳来源为实时
- RAW 功能
常规
- 内存
- 读写性能
- 屏幕分辨率
- 屏幕密度
媒体性能等级 30
MPC 30 包含 MPC 31 的部分要求,使开发者能够在较早但仍功能强大的设备上提供定制体验。具体的性能等级要求发布在 Android 11 CDD 中。
为您推荐
- 注意:当 JavaScript 关闭时显示链接文本
- 应用启动时间