在哪里也可以找到管理 SurfaceView 的线程
Where to locate thread that manages the SurfaceView too
我有一个 activity 承载一些视图:SurfaceView、SeekBar 和一些按钮。 SurfaceView 显示动画,其他视图控制动画的一些参数。我在activity 里面创建了一个Thread 来运行 对所有视图进行所有这些操作。
但是在网络上我总能找到 SurfaceView 的例子,它们有自己的线程,以便在 surfaceCreated() 中启动它并在 surfaceDestroyed() 中停止它,等等。但是我的线程不仅仅用于SurfaceView 因为其他视图参与或与动画交互。那么,我必须把线程放在哪里?在 SurfaceView class 中(以及如何从那里访问其他视图)?或者像我一样在Activityclass?
SurfaceView 和 Activity 生命周期之间的交互并不简单。可以在 in an appendix to the graphics architecture doc.
中找到对这些问题的完整讨论
This raises a fundamental question when using a separate renderer thread with SurfaceView: Should the lifespan of the thread be tied to that of the Surface or the Activity? The answer depends on what you want to have happen when the screen goes blank. There are two basic approaches: (1) start/stop the thread on Activity start/stop; (2) start/stop the thread on Surface create/destroy.
您可以在 Grafika. For example, the HardwareScalerActivity uses approach #2, creating and joining the render thread in the Surface callbacks. TextureFromCameraActivity 使用方法 #1 中找到这两种方法的示例,因为它也在管理相机。在这两种情况下,您都需要注意一些边缘情况,例如HardwareScalerActivity 启动渲染线程,然后在继续之前等待它完成初始化。
我有一个 activity 承载一些视图:SurfaceView、SeekBar 和一些按钮。 SurfaceView 显示动画,其他视图控制动画的一些参数。我在activity 里面创建了一个Thread 来运行 对所有视图进行所有这些操作。
但是在网络上我总能找到 SurfaceView 的例子,它们有自己的线程,以便在 surfaceCreated() 中启动它并在 surfaceDestroyed() 中停止它,等等。但是我的线程不仅仅用于SurfaceView 因为其他视图参与或与动画交互。那么,我必须把线程放在哪里?在 SurfaceView class 中(以及如何从那里访问其他视图)?或者像我一样在Activityclass?
SurfaceView 和 Activity 生命周期之间的交互并不简单。可以在 in an appendix to the graphics architecture doc.
中找到对这些问题的完整讨论This raises a fundamental question when using a separate renderer thread with SurfaceView: Should the lifespan of the thread be tied to that of the Surface or the Activity? The answer depends on what you want to have happen when the screen goes blank. There are two basic approaches: (1) start/stop the thread on Activity start/stop; (2) start/stop the thread on Surface create/destroy.
您可以在 Grafika. For example, the HardwareScalerActivity uses approach #2, creating and joining the render thread in the Surface callbacks. TextureFromCameraActivity 使用方法 #1 中找到这两种方法的示例,因为它也在管理相机。在这两种情况下,您都需要注意一些边缘情况,例如HardwareScalerActivity 启动渲染线程,然后在继续之前等待它完成初始化。