什么时候是必要的,或者更好地使用SurfaceView而不是View?
当前回答
SurfaceView是Android中的自定义视图,可用于在其中绘制。 视图和SurfaceView之间的主要区别是视图是在 UI线程,用于所有用户交互。 如果你想要快速更新UI并呈现大量的信息 那么,SurfaceView是一个更好的选择。
但是SurfaceView有一些内部技术:
1. 它们不是硬件加速的。 2. 当您调用invalidate或postInvalidate()方法时,正常视图将被呈现,但这并不意味着视图将被呈现 立即更新(将发送一个垂直同步,由操作系统决定何时 它得到更新。SurfaceView可以立即更新。 3.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.
以下是我注意到的几点:
SurfaceViews包含一个很好的渲染机制,它允许线程在不使用处理程序的情况下更新表面的内容(对动画很好)。 Surfaceviews不能是透明的,它们只能出现在视图层次结构中的其他元素后面。 我发现它们比渲染到视图上的动画要快得多。
有关更多信息(以及一个很好的使用示例),请参阅SDK中的LunarLander项目 的例子部分。
视图都绘制在相同的GUI线程上,该线程也用于所有用户交互。
所以,如果你需要快速更新GUI,或者渲染花费太多时间,影响用户体验,那么就使用SurfaceView。
SurfaceView是Android中的自定义视图,可用于在其中绘制。 视图和SurfaceView之间的主要区别是视图是在 UI线程,用于所有用户交互。 如果你想要快速更新UI并呈现大量的信息 那么,SurfaceView是一个更好的选择。
但是SurfaceView有一些内部技术:
1. 它们不是硬件加速的。 2. 当您调用invalidate或postInvalidate()方法时,正常视图将被呈现,但这并不意味着视图将被呈现 立即更新(将发送一个垂直同步,由操作系统决定何时 它得到更新。SurfaceView可以立即更新。 3.SurfaceView有一个已分配的表面缓冲区,因此它的开销更大
主要的区别是SurfaceView可以被背景头绘制,但Views不能。 SurfaceViews使用更多的资源,所以你不想使用它们,除非你必须。
推荐文章
- 在Android 5棒棒糖中,通知栏图标变成白色
- 从URI获取真实路径,Android奇巧新的存储访问框架
- 如何检查JSON键是否存在?
- ImageView -有高度匹配宽度?
- 如何确定在android文件的MIME类型?
- 这是在Android中获取用户位置的好方法
- Android从左到右幻灯片动画
- 如何检索视图的维度?
- 如何改变菜单项的文本颜色在安卓?
- Android选择器和文本颜色
- 视图绑定-我如何获得包含布局的绑定?
- 在Android Studio中改变矢量资产的填充颜色
- 在构建中编写注释的语法是什么?gradle文件?
- 如何以编程方式添加按钮色调
- 用Android Studio进行调试永远停留在“等待调试器”状态