什么时候是必要的,或者更好地使用SurfaceView而不是View?


当前回答

以下是我注意到的几点:

SurfaceViews包含一个很好的渲染机制,它允许线程在不使用处理程序的情况下更新表面的内容(对动画很好)。 Surfaceviews不能是透明的,它们只能出现在视图层次结构中的其他元素后面。 我发现它们比渲染到视图上的动画要快得多。

有关更多信息(以及一个很好的使用示例),请参阅SDK中的LunarLander项目 的例子部分。

其他回答

主要的区别是SurfaceView可以被背景头绘制,但Views不能。 SurfaceViews使用更多的资源,所以你不想使用它们,除非你必须。

视图都绘制在相同的GUI线程上,该线程也用于所有用户交互。

所以,如果你需要快速更新GUI,或者渲染花费太多时间,影响用户体验,那么就使用SurfaceView。

以下是我注意到的几点:

SurfaceViews包含一个很好的渲染机制,它允许线程在不使用处理程序的情况下更新表面的内容(对动画很好)。 Surfaceviews不能是透明的,它们只能出现在视图层次结构中的其他元素后面。 我发现它们比渲染到视图上的动画要快得多。

有关更多信息(以及一个很好的使用示例),请参阅SDK中的LunarLander项目 的例子部分。

更新05/09/2014

好的。我们现在有正式文件了。它以一种更好的方式讲述了我所提到的一切。


点击这里阅读更多细节。

是的,主要的区别是surfaceView可以在后台线程上更新。然而,还有更多你可能关心的东西。

surfaceView has dedicate surface buffer while all the view shares one surface buffer that is allocated by ViewRoot. In another word, surfaceView cost more resources. surfaceView cannot be hardware accelerated (as of JB4.2) while 95% operations on normal View are HW accelerated using openGL ES. More work should be done to create your customized surfaceView. You need to listener to the surfaceCreated/Destroy Event, create an render thread, more importantly, synchronized the render thread and main thread. However, to customize the View, all you need to do is override onDraw method. The timing to update is different. Normal view update mechanism is constraint or controlled by the framework:You call view.invalidate in the UI thread or view.postInvalid in other thread to indicate to the framework that the view should be updated. However, the view won't be updated immediately but wait until next VSYNC event arrived. The easy approach to understand VSYNC is to consider it is as a timer that fire up every 16ms for a 60fps screen. In Android, all the normal view update (and display actually but I won't talk it today), is synchronized with VSYNC to achieve better smoothness. Now,back to the surfaceView, you can render it anytime as you wish. However, I can hardly tell if it is an advantage, since the display is also synchronized with VSYNC, as stated previously.

SurfaceView是Android中的自定义视图,可用于在其中绘制。 视图和SurfaceView之间的主要区别是视图是在 UI线程,用于所有用户交互。 如果你想要快速更新UI并呈现大量的信息 那么,SurfaceView是一个更好的选择。

但是SurfaceView有一些内部技术:

1. 它们不是硬件加速的。 2. 当您调用invalidate或postInvalidate()方法时,正常视图将被呈现,但这并不意味着视图将被呈现 立即更新(将发送一个垂直同步,由操作系统决定何时 它得到更新。SurfaceView可以立即更新。 3.SurfaceView有一个已分配的表面缓冲区,因此它的开销更大