View, SurfaceView, GlsurfaceView relationship and difference

If your game does not eat CPU, it is better to use View, which conforms to the standard Android operation mode, and the system determines the timing of refreshing the surface.
But if unfortunately, you can’t prevent your program from eating the CPU, you have to use SurfaceView to forcefully refresh the surface, otherwise the UI process of the system may not be able to grab your CPU-eating threads.
Of course, there are more than these two methods to refresh the Surface, these two are just common methods for pure Java applications.
The most essential difference between SurfaceView and View is that surfaceView can redraw the screen in a new separate thread while View must update the screen in the main thread of the UI.
Then in U I’s main thread to update the picture 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 because it is new Update the screen in the thread so it will not block your UI main 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 relatively longer, which will not affect.
2 Proactively 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.
Generally, SurfaceView is sufficient for 2D game development, because it is also a canvas specially extended by Google for 2D game development
  Use ordinary game canvas (2D special game canvas in Android) to draw pictures, and then draw pictures in GLSurfaceView (3D game special canvas in Android) ) In the comparison of rendered pictures, it is found that the efficiency of GLSurfaceView is higher than S 30 times that of surfaceView; the efficiency of GLSurfaceView is mainly due to the GPU acceleration of the machine hardware, and now flash technology also has GPU acceleration technology;
   Here is a summary:
   General 2D Tour SurfaceView is enough for the show, so don’t think that you have to use GLSurfaceView (openGL) for everything, and the drawback of GLSurfaceView is poor adaptability, because many models do not have GPU acceleration.

Leave a Comment

Your email address will not be published.