什么时候是必要的,或者更好地使用SurfaceView而不是View?
当前回答
主要的区别是SurfaceView可以被背景头绘制,但Views不能。 SurfaceViews使用更多的资源,所以你不想使用它们,除非你必须。
其他回答
视图都绘制在相同的GUI线程上,该线程也用于所有用户交互。
所以,如果你需要快速更新GUI,或者渲染花费太多时间,影响用户体验,那么就使用SurfaceView。
更新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可以被背景头绘制,但Views不能。 SurfaceViews使用更多的资源,所以你不想使用它们,除非你必须。
为什么使用SurfaceView而不是经典的View类…
一个主要原因是SurfaceView可以快速渲染屏幕。
简单地说,SV更有能力管理时间和渲染动画。
为了更好地理解什么是SurfaceView,我们必须将它与View类进行比较。
有什么不同……请看视频中的简单解释
https://m.youtube.com/watch?feature=youtu.be&v=eltlqsHSG30
对于视图,我们有一个主要问题....渲染动画的时间。
通常从Android运行时系统调用onDraw()。
因此,当Android运行时系统调用onDraw()时,应用程序无法控制
显示的时间,这对动画很重要。我们在时间上有差距
应用(游戏邦注:即我们的游戏)和Android运行时系统之间的关系。
SV可以通过专用线程调用onDraw()。
因此:应用程序控制时间。所以我们可以显示动画的下一个位图图像。
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
推荐文章
- 视图绑定-我如何获得包含布局的绑定?
- 在Android Studio中改变矢量资产的填充颜色
- 在构建中编写注释的语法是什么?gradle文件?
- 如何以编程方式添加按钮色调
- 用Android Studio进行调试永远停留在“等待调试器”状态
- Openssl不被视为内部或外部命令
- 无法执行dex:在Eclipse中超过GC开销限制
- 如何以编程方式将视图添加到视图
- 单击url会打开默认浏览器
- 使用Retrofit刷新OAuth令牌,而不修改所有调用
- 多个dex文件定义了landoid /support/v4/accessibilityservice/AccessibilityServiceInfoCompat
- 如何获得动作栏的高度?
- 从活动外部调用startActivity() ?
- createScaledBitmap的过滤器参数做什么?
- 为什么我在使用adb时访问数据文件夹被拒绝?