SurfaceView and View’s most essential difference

surfaceView can redraw the screen in a new separate thread, and the View must update the screen in the main thread of the UI. Then updating the screen in the main thread of the UI may cause problems. For example, if you update the screen for too long, then your main UI thread will be blocked by the function you are drawing. Then it will not be able to respond to messages such as keystrokes and touch screens. When using surfaceView, it will not block your UI main thread because it updates the screen in a new thread. But this also brings another problem, that is, event synchronization. For example, if you touch the screen, you need thread processing in surfaceView. Generally, you need to have an event queue design to save touch events. This is a little more complicated because it involves thread synchronization.

So based on the above, according to the characteristics of the game, it is generally divided into two categories:

1 Passively update the screen. Such as chess, this kind of use view is just fine. Because the screen update depends on onTouch to update, you can use invalidate directly. Because in this case, the time required for this touch and the next touch is longer and will not have any effect.

2 Active update. For example, a person is constantly running. This requires a separate thread to continuously redraw the state of the person to avoid blocking the main UI thread. So obviously the view is not suitable and needs surfaceView to control it.

Introduction to SurfaceView< /strong>

In general, the View of the application is drawn in the same GUI thread. This main application thread is also used to handle all user interactions (for example, button clicks or text input).
When the View’s UI needs to be updated quickly, or when the rendering code blocks the GUI thread for too long, SurfaceView is the best choice to solve the above problems. SurfaceView encapsulates a Surface object instead of Canvas. This is important because Surface can use background threads to draw. This is especially useful for resource-sensitive operations, or where fast updates or high frame rates are required, such as using 3D graphics, creating games, or real-time preview cameras.
The cost of drawing independently of the GUI thread is additional memory consumption, so although it is an effective way to create a customized View–sometimes it is even necessary, but use Still have to be cautious when using Surface View.


When should I use SurfaceView?

SurfaceView uses the same way as any class derived from View Exactly the same. You can apply animations like other Views and put them in the layout. The Surface encapsulated by SurfaceView supports drawing using all the standard Canvas methods described earlier in this chapter, as well as the full OpenGL ES library. Using OpenGL, you can draw any supported 2D or 3D object on the Surface. Compared with simulating the same effect on a 2D canvas, this method can rely on hardware acceleration (when available) to greatly improve performance. For displaying dynamic 3D images, for example, those applications that use Google Earth functions, or those interactive games that provide an immersive experience, SurfaceView is particularly useful. It is also the best choice for real-time display of camera previews.

Leave a Comment

Your email address will not be published.