When is it necessary, or better to use a SurfaceView
instead of a View
?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
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.
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.
https://youtu.be/kRqsoApOr9U
https://youtu.be/Ji84HJ85FIQ
https://youtu.be/U8igPoyrUf8
Views are all drawn on the same GUI thread which is also used for all user interaction.
So if you need to update GUI rapidly or if the rendering takes too much time and affects user experience then use
SurfaceView
.A
SurfaceView
is a custom view in Android that can be used to drawn inside it.The main difference between a
View
and aSurfaceView
is that a View is drawn in theUI 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.
But there are a few technical insides to the
SurfaceView
:1. They are not hardware accelerated.
2. Normal views are rendered when you call the methods
invalidate
orpostInvalidate()
, but this does not mean the view will be immediately updated (AVSYNC
will be sent, and the OS decides when it gets updated. TheSurfaceView
can be immediately updated.3. A SurfaceView has an allocated
surface buffer
, so it is more costlyupdated 05/09/2014
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 has dedicate surface buffer while all the view share one surface buffer that is allocated by ViewRoot. In another word, surfaceView cost more resources.
surfaceView can not 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.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 VYSNC 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 VSNC, as stated previously.Another definition would be:
Source: The Big Nerd Ranch Guide
The main difference is that
SurfaceView
can be drawn on by background theads butViews
can't.SurfaceViews
use more resources though so you don't want to use them unless you have to.