You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Compose与XML中测量屏幕渲染时间及启动性能?

测量Compose与XML应用的启动及页面首帧耗时方法

一、应用首次启动时间测量

1. Android Studio Profiler 可视化测量

  • 打开Android Studio,连接测试设备,运行目标应用
  • 切换到Profiler窗口(点底部工具栏的Profiler,或用快捷键Alt+Shift+F12)
  • 选中对应进程,点击CPU Profiler旁的下拉菜单,选择Startup Profiler
  • 点击Record,手动启动未运行的应用
  • 录制结束后,时间线里标记为First Frame的节点,就是从进程启动到首帧渲染完成的耗时

2. 命令行(ADB)精确测量

用am start命令配合参数,系统会直接输出启动耗时:

adb shell am start -W 包名/主Activity全类名

输出里的关键指标:

  • TotalTime:从调用start到应用首帧显示的总耗时(含系统处理时间)
  • WaitTime:系统启动应用的总耗时(含其他进程干扰时间)
  • ThisTime:应用自身启动的耗时(仅内部代码执行时间)
    测试前先杀进程:adb shell am force-stop 包名

二、页面首帧渲染耗时测量

1. XML(单Activity+多Fragment)页面耗时

方法1:代码埋点

在Fragment生命周期里埋点,结合Choreographer监听首帧:

class TestFragment : Fragment() {
    private var startTime: Long = 0

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        startTime = System.currentTimeMillis()
        return inflater.inflate(R.layout.fragment_test, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        // 监听首帧绘制完成
        Choreographer.getInstance().postFrameCallback { frameTimeNanos ->
            val cost = (frameTimeNanos / 1000000) - startTime
            Log.d("FragmentPerf", "页面首帧耗时:${cost}ms")
        }
    }
}

方法2:Android Studio 导航耗时追踪

  • 打开Run/Debug Configurations,给目标应用配置勾选Advanced Profiling
  • 运行应用,切换到Profiler的CPU Profiler,录制页面跳转过程
  • 在调用栈里找Fragment从onCreateView到onDraw完成的时间区间,计算耗时

2. Compose(Navigation Component)页面耗时

方法1:代码埋点+LaunchedEffect监听首帧

在可组合函数里埋点,用LaunchedEffect和Choreographer:

@Composable
fun TestScreen() {
    val startTime = remember { System.currentTimeMillis() }
    LaunchedEffect(Unit) {
        Choreographer.getInstance().postFrameCallback { frameTimeNanos ->
            val cost = (frameTimeNanos / 1000000) - startTime
            Log.d("ComposePerf", "页面首帧耗时:${cost}ms")
        }
    }
    // 页面内容
    Column(modifier = Modifier.fillMaxSize()) {
        Text(text = "Test Screen")
    }
}

方法2:Navigation 导航耗时监听

用Navigation的OnDestinationChangedListener监听跳转开始时间,结合首帧回调:

val navController = rememberNavController()
var navStartTime: Long = 0

navController.addOnDestinationChangedListener { _, destination, _ ->
    if (destination.route == "test_screen") {
        navStartTime = System.currentTimeMillis()
        Choreographer.getInstance().postFrameCallback { frameTimeNanos ->
            val cost = (frameTimeNanos / 1000000) - navStartTime
            Log.d("NavPerf", "页面跳转+首帧耗时:${cost}ms")
        }
    }
}

三、统一测试注意事项

  • 测试设备要稳定:关掉后台无关应用,禁用省电模式,保证电量充足
  • 重复测至少5次,取平均值减少误差
  • 用release包(或开R8混淆的debug包)测试,避免debug模式的性能损耗
  • 关闭Android Studio的Instant Run,确保安装的是完整包

内容的提问来源于stack exchange,提问作者SoulReaver313

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 16:36:43