Core Components of Android Game Development Framework
Introduction to core components
Introduction to SurfaceView
Introduction to SurfaceView
- SurfaceView is a view with Surface. It is a View, a subclass of View, so like other Views, it can display things on the screen and receive user input, and has the life cycle of View Callback functions, such as onMeasure, onLayout, onDraw, onTouchEvent, etc.
- SurfaceView has an independent Surface (independent and window surface), which allows child threads to draw things on an independent Surface. To draw the SurfaceView interface, this sub-thread is called the rendering thread, but in order to display the things on the independent Surface on the View, you need to post a message to the main thread. The purpose is to draw the things on the canvas on the Surface to the View. On the real canvas (on the canvas of the window surface), so that the UI thread can be free to handle user interaction
- Surface may be destroyed, it is only in SurfaceHolder.Callback.surfaceCreated () and SurfaceHolder.Callback.surfaceDestroyed() are valid. This just means that when the Surface is created and destroyed, it will return to the previous two methods, so make sure that the rendering thread accesses a legal and valid surface
- SurfaceHolder.CallBack is set to SurfaceHolder through the addCallback of SurfaceHolder of SurfaceView, let SurfaceView implement CallBack and set it to SurfaceHolder, SurfaceView can monitor the creation and destruction of this independent Surface.
Introduction in sdk
SurfaceView is an inherited class of View, and a Surface dedicated to drawing is embedded in this view. You can control the format and size of this Surface. Surfaceview controls the drawing position of this Surface.
The surface is Z-ordered, which means it is always behind the window where it is. Surfaceview provides a visible area, only part of the content of the surface is visible in this visible area, and the part outside the visible area is not visible.
The layout of the surface is affected by the view hierarchy, and its sibling view nodes will be displayed at the top. This means that the content of the surface
will be obscured by its sibling view. This feature can be used to place overlays (for example, controls such as text and buttons). Note that if there are transparent controls on the surface, then every change of it will cause the framework to recalculate the transparency between it and the top-level controls, which will affect performance.
You can access this surface through the surfaceHolder interface, and the getHolder() method can get this interface.
When the surfaceview becomes visible, the surface is created; before the surfaceview is hidden, the surface is destroyed. This saves resources. If you want to check the timing of surface creation and destruction, you can overload surfaceCreated (SurfaceHolder) and surfaceDestroyed (SurfaceHolder). The core of surfaceView is to provide two threads: UI thread and rendering thread.
Note here:
1. All SurfaceView and SurfaceHolder.Callback methods will be called in the UI thread, which is generally the main thread of the application. Therefore, the various variables to be accessed by the rendering thread should be synchronized.
2. Since the surface may be destroyed, it is only valid between SurfaceHolder.Callback.surfaceCreated() and SurfaceHolder.Callback.surfaceDestroyed(), so make sure that the rendering thread access is legal and valid
surface.
Introduction to SurfaceHolder
SurfaceHolder is a wrapper for SurfaceView’s Surface. It is not only responsible for the callbacks of Surface creation and destruction in the SurfaceHolder.callback interface, but also the key methods LockCanvas() and unLockCanvasAndPost() of Surface. Thread-safe packaging, so SurfaceHolder is the holder of the Surface object, responsible for the invocation of Surface operation methods in the life cycle of the Surface
The dirty rectangle Rect dirty refers to marking this rectangular area The data is invalid, that is, the rectangular area that needs to be rewritten, LockCanvas (Rect dirty), you can specify a rectangular area to redraw part of the data on the Canvas in the Surface.
The relationship between SurfaceView, SurfaceHolder, Surface
SurfaceView steps
- Get the SurfaceHolder corresponding to the SurfaceView, and add a SurfaceHolder.callback object to the SurfaceHolder.
- Create a rendering thread object
- Start drawing graphics on the Surface in the child thread, because SurfaceView does not expose the Surface to us, but only exposes the Surface The wrapper SurfaceHolder, so use the lockCanvas() of SurfaceHolder to get the Canvas of the specified area on the Surface, draw graphics on the Canvas, after drawing, use the unlockCanvasAndPost() method of SurfaceHolder to unlock the Canvas, and let the UI thread put things on the Surface Draw on the Canvas of View