Android SurfaceView 和 View 的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1243433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between SurfaceView and View?
提问by Viktor
When is it necessary, or better to use a SurfaceView
instead of a View
?
什么时候需要或更好地使用 aSurfaceView
而不是 a View
?
回答by Niko Gamulin
Views are all drawn on the same GUI thread which is also used for all user interaction.
视图都绘制在同一个 GUI 线程上,该线程也用于所有用户交互。
So if you need to update GUI rapidly or if the rendering takes too much time and affects user experience then use SurfaceView
.
因此,如果您需要快速更新 GUI 或渲染花费太多时间并影响用户体验,请使用SurfaceView
.
回答by Ralphleon
A few things I've noted:
我注意到的一些事情:
- SurfaceViews contain a nice rendering mechanism that allows threads to update the surface's content without using a handler (good for animation).
- Surfaceviews cannotbe transparent, they can only appear behind other elements in the view hierarchy.
- I've found that they are much faster for animation than rendering onto a View.
- SurfaceViews 包含一个很好的渲染机制,允许线程在不使用处理程序的情况下更新表面的内容(适用于动画)。
- Surfaceviews不能是透明的,它们只能出现在视图层次结构中的其他元素后面。
- 我发现它们的动画比渲染到视图上要快得多。
For more information (and a great usage example) refer to the LunarLander project in the SDK 's examples section.
有关更多信息(和一个很好的使用示例),请参阅 SDK 示例部分中的 LunarLander 项目。
回答by pierrotlefou
updated 05/09/2014
2014 年 5 月 9 日更新
OK. We have official document now.It talked all I have mentioned, in a better way.
好的。我们现在有正式文件。它以更好的方式讲述了我提到的所有内容。
Read more detailed here.
Yes, the main difference is surfaceView can be updated on the background thread. However, there are more you might care.
是的,主要区别在于可以在后台线程上更新 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 orview.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 有专用的表面缓冲区,而所有视图共享一个由 ViewRoot 分配的表面缓冲区。换句话说,surfaceView 需要更多的资源。
SurfaceView 不能硬件加速(从 JB4.2 开始),而普通 View 上的 95% 操作是使用 openGL ES 进行硬件加速的。
应该做更多的工作来创建您自定义的surfaceView。需要监听surfaceCreated/Destroy事件,创建渲染线程,更重要的是同步渲染线程和主线程。但是,要自定义视图,您需要做的就是覆盖
onDraw
方法。- 更新时间不同。普通的视图更新机制是由框架约束或控制的:你
view.invalidate
在UI线程或view.postInvalid
其他线程中调用,向框架指示应该更新视图。但是,视图不会立即更新,而是等到下一个 VSYNC 事件到达。理解 VSYNC 的简单方法是将其视为一个计时器,每 16 毫秒为 60 fps 屏幕启动一次。在Android中,所有正常的视图更新(和实际显示,但我今天不说),都与VSYNC同步,以获得更好的平滑度。现在,回到surfaceView,你可以随心所欲地渲染它。但是,我很难说这是否是一个优势,因为如前所述,显示也与 VSYNC 同步。
回答by jcoder
The main difference is that SurfaceView
can be drawn on by background theads but Views
can't.
SurfaceViews
use more resources though so you don't want to use them unless you have to.
主要区别在于SurfaceView
可以通过背景主题绘制但Views
不能。
SurfaceViews
使用更多资源,因此除非必须,否则您不想使用它们。
回答by Setu Kumar Basak
A SurfaceView
is a custom view in Android that can be used to drawn inside it.
The main difference between a View
and a SurfaceView
is that a View is drawn in the
UI Thread
, which is used for all the user interaction.
If you want to update the UI rapidly enough and render a good amount of information in
it, a SurfaceView is a better choice.
ASurfaceView
是 Android 中的自定义视图,可用于在其中绘制。
aView
和 a之间的主要区别在于SurfaceView
,在 中绘制了一个 View
UI Thread
,用于所有用户交互。
如果你想足够快地更新 UI 并在其中渲染大量信息,SurfaceView 是更好的选择。
But there are a few technical insides to the SurfaceView
:
但是有一些技术内幕SurfaceView
:
1.They are not hardware accelerated.
2.Normal views are rendered when you call the methods invalidate
or postInvalidate()
, but this does not mean the view will be
immediately updated (A VSYNC
will be sent, and the OS decides when
it gets updated. The SurfaceView
can be immediately updated.
3.A SurfaceView has an allocated surface buffer
, so it is more costly
1.它们不是硬件加速的。
2.调用invalidate
or方法时会渲染普通视图postInvalidate()
,但这并不意味着视图会立即更新(VSYNC
发送A ,操作系统决定何时更新。SurfaceView
可以立即更新
。3. SurfaceView 具有分配的surface buffer
,所以成本更高
回答by somenath mukhopadhyay
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 和 view 之间的主要区别之一是要刷新普通视图的屏幕,我们必须从定义视图的同一线程调用 invalidate 方法。但即使我们调用 invalidate,刷新也不会立即发生。它仅在 VSYNC 信号下一次到达后发生。VSYNC 信号是内核生成的信号,每 16.6 毫秒发生一次,也称为每秒 60 帧。所以如果我们想要更多地控制屏幕的刷新(例如非常快速移动的动画),我们不应该使用普通的视图类。
On the other hand in case of surfaceview, we can refresh the screen as fast as we want and we can do it from a background thread. So refreshing of the surfaceview really does not depend upon VSYNC, and this is very useful if we want to do high speed animation. I have few training videos and example application which explain all these things nicely. Please have a look at the following training videos.
另一方面,在 SurfaceView 的情况下,我们可以根据需要快速刷新屏幕,并且可以从后台线程执行此操作。所以刷新surfaceview真的不依赖于VSYNC,如果我们想做高速动画,这非常有用。我有很少的培训视频和示例应用程序可以很好地解释所有这些事情。请观看以下培训视频。
回答by Takis Doris
Why use SurfaceView and not the classic View class...
为什么使用 SurfaceView 而不是经典的 View 类...
One main reason is that SurfaceView can rapidly render the screen.
一个主要原因是 SurfaceView 可以快速渲染屏幕。
In simple words a SV is more capable of managing the timing and render animations.
简而言之,SV 更有能力管理时间和渲染动画。
To have a better understanding what is a SurfaceView we must compare it with the View class.
为了更好地理解什么是 SurfaceView,我们必须将它与 View 类进行比较。
What is the difference... check this simple explanation in the video
有什么区别...在视频中查看这个简单的解释
https://m.youtube.com/watch?feature=youtu.be&v=eltlqsHSG30
https://m.youtube.com/watch?feature=youtu.be&v=eltlqsHSG30
Well with the View we have one major problem....the timing of rendering animations.
对于视图,我们有一个主要问题......渲染动画的时间。
Normally the onDraw() is called from the Android run-time system.
通常从 Android 运行时系统调用 onDraw()。
So, when Android run-time system calls onDraw() then the application cant control
所以,当Android运行时系统调用onDraw()时,应用程序无法控制
the timing of display, and this is important for animation. We have a gap of timing
显示的时间,这对于动画很重要。我们有时间差
between the application (our game) and the Android run-time system.
在应用程序(我们的游戏)和 Android 运行时系统之间。
The SV it can call the onDraw() by a dedicated Thread.
SV 它可以通过专用线程调用 onDraw()。
Thus: the application controls the timing. So we can display the next bitmap image of the animation.
因此:应用程序控制时间。这样我们就可以显示动画的下一个位图图像。