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


主要的区别是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有一个已分配的表面缓冲区,因此它的开销更大


One of the main differences between surfaceview and view is that to refresh the screen for a normal view we have to call invalidate method from the same thread where the view is defined. But even if we call invalidate, the refreshing does not happen immediately. It occurs only after the next arrival of the VSYNC signal. VSYNC signal is a kernel generated signal which happens every 16.6 ms or this is also known as 60 frame per second. So if we want more control over the refreshing of the screen (for example for very fast moving animation), we should not use normal view class.

另一方面,在surfaceview的情况下,我们可以尽可能快地刷新屏幕,我们可以从后台线程完成。surfaceview的刷新真的不依赖于VSYNC,如果我们想做高速动画,这是非常有用的。我有一些培训视频和示例应用程序,可以很好地解释所有这些事情。请看下面的培训视频。

https://youtu.be/kRqsoApOr9U

https://youtu.be/Ji84HJ85FIQ

https://youtu.be/U8igPoyrUf8


为什么使用SurfaceView而不是经典的View类…

一个主要原因是SurfaceView可以快速渲染屏幕。

简单地说,SV更有能力管理时间和渲染动画。

为了更好地理解什么是SurfaceView,我们必须将它与View类进行比较。

有什么不同……请看视频中的简单解释

https://m.youtube.com/watch?feature=youtu.be&v=eltlqsHSG30

对于视图,我们有一个主要问题....渲染动画的时间。

通常从Android运行时系统调用onDraw()。

因此,当Android运行时系统调用onDraw()时,应用程序无法控制

显示的时间,这对动画很重要。我们在时间上有差距

应用(游戏邦注:即我们的游戏)和Android运行时系统之间的关系。

SV可以通过专用线程调用onDraw()。

因此:应用程序控制时间。所以我们可以显示动画的下一个位图图像。