Android surfaceview (1)
Surface, the word is floating on the surface, so surfaceview is floating on the surface of the view. If this is the case, it is estimated that someone will make a brick. However, even though this cannot be said, it is still somewhat related to the name. Surface is a visible area.
The view we see on the screen shows the screen and a memory area in the memory. When drawing, the display hardware, such as the video card, draws the graphic data in the memory area to the screen. Therefore, it is easier to understand these things from the memory perspective.
Surface is a visible part of surfaceview. We know that the image we see on the screen is two-dimensional. What we see is the length and width. In fact, it is actually three-dimensional inside, and another dimension is layer. We can see this situation when drawing with visio. One graph will cover the other because it is on the upper layer. If you have experience with AutoCAD, it is easier to understand this. The images we see are actually stacked together by multiple layers. These graphic elements are completely invisible, partially visible, or completely visible.
In this case, the surface can be understood as follows: it is a block of memory, it is invisible to surfaceview, and the drawing operation acts on it, then it is drawn to the screen by a display controller such as a video card.
What is surface? There are some concepts. Because it corresponds to a memory zone, we all know that the objects in the memory zone have a lifecycle, and can be dynamically applied for creation and destruction, of course, it may also be updated. As a result, there are operations acting on this memory zone. These operations are surfaceCreated/Changed/Destroyed. Put the three operations together, that is, callback. Therefore, in many examples, we can see that there will be callback.
Callback refers to callback, which means you can do some work on your own, but you don't take the initiative to do it. Instead, you register it with someone else. When someone else needs it, they will ask me to do it. For example, A construction engineer A can build, renovate, or demolish A house. But he did not take the initiative to do these things, but went to developer B to register these three capabilities. Of course, he packed these three capabilities, called A's capabilities. When B has A job, it is called A to build A house, renovate A house, or demolish A building. In this example, the capability package is callback.
This example explains some surface-related things. The callback has already been mentioned. Let's talk about other things below. If the surface is a house, who owns surfaceHolder? In this example, it is like building Team. SurfaceCreated is used to build a house, and the house demolition corresponds to surfaceDestroyed, And the decoration corresponds to surfaceChanged.
The surface has a life-saving period. It is like a house has a life-saving period. It exists after it is built, and it will not exist after it is split. Decoration must happen between them. Similarly, the change of the surface must occur between created and destroyed.
Surfaceview knows who the surface holder is. When surfaceview is generated, it calls getHolder to get the holder, and then holder calls addCallback to register the three callback functions.
Holder has control over the surface. In many programs, another thread is created in the surfaceCreated function implementation. So here there are two threads: one is the UI thread and the other is responsible for drawing. The drawing thread is created when the UI thread calls surfaceCreated, and is put back to the thread pool when surfaceDestroyed calls it. The drawing thread is responsible for drawing the image.
In this model, the UI thread and the drawing thread perform their respective duties. The former is mainly responsible for interaction with users, while the latter is responsible for drawing graphics. In this way, if the drawing takes a long time, the user's input will not be blocked.
We know that threads share memory data, so surface is shared between two threads. Therefore, in order to avoid surface operations when drawing, the UI thread needs to lock the surface before drawing. This job is done by holder. holder locks a holder. lockCanvas in the surface, which is called canvas. Then, after painting, unlockCanvasAndPost is unlocked.
In applications, a drawing can be a one-time or scheduled drawing triggered by a timer. The run method in the runnable class is implemented. The runnable class defined in Java is not unique to android. For details, refer to the referrence of Java.
The biggest benefit of this model is that drawing does not depend on the UI thread and does not block the UI thread. The simple view relies on the drawing of the UI thread. For image display updates that depend entirely on user input, you can use view. However, if you can automatically draw images without waiting for user input, surfaceview is undoubtedly a better choice.
See the figure