surfaceview

Core Components of Android Game Development Framework

Write the picture description here

Introduction to core components

Introduction to SurfaceView

Introduction to SurfaceView

  1. 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.
  2. 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
  3. 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
  4. 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

Write the picture description here

SurfaceView steps

  1. Get the SurfaceHolder corresponding to the SurfaceView, and add a SurfaceHolder.callback object to the SurfaceHolder.
  2. Create a rendering thread object
  3. 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

Demo used by SurfaceView

public class GameUI extends  SurfaceView implements SurfaceHolder.Callback { private< /span> SurfaceHolder holder; private RenderThread renderThread; private boolean isDraw = false;// Control The drawn switch public GameUI(Context context) { super(context); holder = this.getHolder(); holder.addCallback(this); renderThread = new RenderThread();} @Override public< /span> void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {} @Override public void surfaceCreated(SurfaceHolder holder) {isDraw = true; renderThread.start();} @Override public< /span> void surfaceDestroyed< /span>(SurfaceHolder holder) {isDraw = false;} /** * Thread for drawing interface* * @author Administrator * */ private  class RenderThread extends Thread { @Override public void run() {// Keep drawing the interface while (isDraw) {drawUI();} super.run();}} /* * * Interface drawing*/ public void drawUI() {Canvas canvas = holder.lockCanvas(); try {drawCanvas(canvas); } catch (Exception e) {e.printStackTrace(); } finally {holder.unlockCanvasAndPost(canvas);}} < span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private void drawCanvasdrawCanvas spa n>(Canvas canvas) {// Draw the required graphics on the canvas< /span> }}

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 1496 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.