This article is for reading http://blog.csdn.net/xiaominghimi/article/details/6089594 's notes.
The main role in Android games, in addition to the control class is the display class. What's involved in Android is the view class, and other subclasses of the Surfaceview class and surfaceview that inherit from it.
This is the first view and Surfaceview. The direct subclasses of Surfaceview are Glsurfaceview and Videoview, and you can see that GL and video playback and camera cameras generally use surfaceview.
Surfaceview can control the format of the surface, such as size, the location of the display in the screen, the most critical is to provide the Surfaceholder class, using the Getholder method to obtain, related to Lockcanvas (), Removecallback (), Unlockcanvasandpost () controls graphics and drawing.
When using Surfaceview, it is generally necessary to monitor the creation, destruction, and alteration of the surfaceholder.callback. The interface has three callback methods, namely surfacechanged (surfaceholder holder, int format, int width, int height), surfacecreated (Surfaceholder Holder), surfacedestroyed (Surfaceholder holder).
For surface-related purposes, the Android low-level also provides GPU acceleration, so generally real-time applications are mostly built using Surfaceview instead of directly from view, while later doing Android 3D The Glsurfaceview in OpenGL is also implemented from this class.
The essential difference between surfaceview and view is that Surfaceview can draw a picture in a new, separate thread, and Viwe must update the screen in the UI thread.
Updating the screen in the UI can cause an unresponsive problem due to thread blocking, and updating the screen in a non-UI thread can also cause an event synchronization problem. For example, touch the screen, need to surfaceview thread processing, generally need to have an event queue, which involves the synchronization of threads.
Based on the above, according to the game features are divided into two categories:
1, the passive update interface. such as chess games, with view on it. Because the screen update relies on touch events, you can use the invalidate () method directly. Because in this case, the interval between updates is longer.
2, active update. such as the movement of characters in the game. This requires a separate thread to continually redraw the human state and avoid blocking the UI thread, so Surfaceview control is required.
The Surfaceview class in Android is a double-buffering mechanism, with high efficiency and improved functionality.