The relationship between SurfaceView and Surfaceholder

1. 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 the part of the surface in this visible area is visible, and the part outside the visible area is not visible. The layout of the surface is affected by the hierarchical relationship of the views, 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 views. 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 effects of 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 see when the surface is created and destroyed, you can override 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 should be called in the UI thread, which is generally the main thread of the application. 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 to render The thread accesses a valid surface.

SurfaceHolder
2. A class SurfaceHolder is used here, which can be used as a surface controller to manipulate the surface. Process the effects and animations drawn on its Canvas, control the surface, size, pixels, etc.
A few methods that need attention:< br style="padding:0px; margin:0px"> (1), abstract void addCallback(SurfaceHolder.Callback callback);
// Give the current holder of SurfaceView a callback object.
(2), abstract Canvas lockCanvas(); span>
// Lock the canvas, generally you can return through it after locking The canvas object Canvas, draw pictures and other operations on it.
(3), abstract Canvas lockCanvas(Rect dirty);
// Lock a certain area of ​​the canvas for drawing, etc. .Because after drawing the picture, the following unlockCanvasAndPost will be called to change the display content.
// Relative to some games with high memory requirements , You don’t need to redraw the pixels in other areas except dirty, which can increase the speed.
(4), abstract void unlockCanvasAndPost(Canvas canvas);
// End the lock drawing and submit the changes.

Leave a Comment

Your email address will not be published.