android - RenderThread 与 UI 线程

标签 android multithreading

来自 Android 线程 doc :

you must not manipulate your UI from a worker thread—you must do all manipulation to your user interface from the UI thread

所以我相信屏幕上的所有内容都是由UI 线程 呈现的。但在 Android Lollipop 中他们有 introduced一个RenderThread:

A new system-managed processing thread called RenderThread keeps animations smooth even when there are delays in the main UI thread

它是如何工作的? RenderThread 是否使用UI 线程 在屏幕上呈现动画(具有新属性的 View )?如果是这样,它为什么不阻塞 UI 线程

最佳答案

RenderThread 依赖于 UI Thread 但它确实与最后提到的并行运行。

它的主要工作是在 GPU 上运行昂贵的计算,以清空 UI 线程 的繁重负载。


How does it work?

基本上,UI 线程 充当作业调度程序。它准备要在 RenderThread 上执行的命令管道。

GPU 不知道什么是动画;它只能理解基本命令,例如:

  • 平移(x,y,z)
  • 旋转(x,y)

或基本绘图工具:

  • drawCircle(centerX, centerY, radius, paint)
  • drawRoundRect(left, top, right, bottom, cornerRadiusX, cornerRadiusY, paint)

它们结合在一起形成了您在屏幕上看到的复杂动画。

Does the RenderThread use the UI thread to render animations (Views with new properties) on the screen?

不,它是异步运行的

If so, why doesn't it block the UI thread?

docs说明渲染分两个阶段执行:

  1. View#draw -> UI 线程
  2. DrawFrame -> RenderThread,它基于 View#draw 阶段执行工作。

在较低的层次上,当使用硬件加速时,延迟的渲染DisplayListCanvas执行。 .

在这个 Canvas 实现中,您可以找到上述绘图命令,例如 drawCircle

因此,DisplayListCanvas 也是 RenderNodeAnimator 的绘图目标,它运行基本的动画命令(translatescalealpha、...)。

关于android - RenderThread 与 UI 线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48618444/

相关文章:

android - Unresolved reference DaggerApplicationComponent

java - Android 应用程序在模拟器上运行,在手机上崩溃

android - LoadedApk$ServiceDispatcher.mContext 正在泄漏

c++ - Qt 线程等待来自 GUI 的输入

java - Java 并行流中的异常传播

C 程序卡住,不进入 main()

android - 在 Android 中使用 Spinner 选择作为值

java - 查找排序数组的原始索引

c# - .NET 的多线程库

java - Android AsyncTask 如何设置定时器