Returned from http://www.cnblogs.com/xuling/archive/2011/06/06/android.html
SurfaceView is an inherited class of View, in this view A Surface dedicated to drawing is embedded. You can control the format and size of this Surface. Surfaceview controls the drawing position of this Surface.
Surface is Z-ordered, which means that 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. It should be noted 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 that the rendering thread accesses a legal and valid surface.
Next, talk about your understanding of it
1. Definition
Image data can be obtained directly from hardware interfaces such as memory or DMA. It is a very important drawing container.
Its feature is that it can draw on the screen in a thread other than the main thread. This can avoid blocking the main thread when the drawing task is heavy, thereby improving the response speed of the program. SurfaceView is often used in game development, and the background, characters, animation, etc. in the game are drawn on the canvas as much as possible.
2. Implementation
First inherit SurfaceView and implement SurfaceHolder.Callback interface
Reason for using interface: Because there is a principle for using SurfaceView, all drawing work must be done on Surface It can only be started after creation (Surface—surface, this concept is often mentioned in graphics programming. Basically we can regard it as a mapping of video memory and write to the content of Surface
It can be directly copied to the video memory to display Out, which makes the display speed very fast), and must end before the Surface is destroyed. So surfaceCreated and surfaceDestroyed in Callback become the boundary of drawing processing code.
Methods to be rewritten
(1)public void surfaceChanged(SurfaceHolder holder, int format,int width,int height){}
/ / Fired when the size of the surface changes
(2) public void surfaceCreated(SurfaceHolder holder){}
//in Fired when created, generally Call the drawing thread here.
(3)public void surfaceDestroyed(SurfaceHolderholder){}
//It will be triggered when it is destroyed. Generally, the picture will be drawn here The thread is stopped and released.
The whole process: Inherit SurfaceView and implement the SurfaceHolder.Callback interface —-> SurfaceView.getHolder() to obtain the SurfaceHolder object —-> SurfaceHolder.addCallback(callback) to add the callback function —->SurfaceHolder.lockCanvas() to obtain the Canvas Object and lock the canvas—->Canvas painting—->SurfaceHolder.unlockCanvasAndPost(Canvas canvas) end the lock painting, submit the changes, and display the graphics.
3. SurfaceHolder Here is a class SurfaceHolder that can be used It acts as a surface controller and is used to manipulate the surface. Process the effects and animations drawn on its Canvas, control the surface, size, pixels, etc. Several methods to pay attention to: (1), abstract void addCallback(SurfaceHolder.Callback callback); // Give the current holder of SurfaceView a callback object. (2), abstract Canvas lockCanvas(); // Lock the canvas. Generally, after locking, you can use the returned canvas object Canvas to draw pictures 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. // Compared with some games with high memory requirements, you can increase the speed without redrawing the pixels in other areas except dirty. (4), abstract void unlockCanvasAndPost(Canvas canvas); // End the lock drawing and submit the changes.