Watching "Android game development" (Pro Android games) on the weekend, the framework of the game, one of the important concepts Surfaceview, feel not very understanding, so spent a little time to study the next, write down their experience.
Surface, the meaning of this word is floating on the surface, then the surfaceview is floating on the surface of the view. If this is really the case, it is estimated that someone will be making bricks. However, although the words can not say that, take the name, how much is still a bit of a relationship. Surface is a visible area. The view that we see on the screen, what we see on the screen, is a memory area in memory. At the time of drawing, it is the hardware shown, such as a video card, that draws this piece of graphic data from the memory area to the screen. So, from the memory point of view of these things, it will be better to understand. surface is a visible part of the Surfaceview. We know that the graphics we see on the screen are two-dimensional, we see the length and width, in fact, in the interior is actually three-dimensional, and another dimension, is layer layers. We'll see this in Visio drawing, and one graphic will obscure the other because it's on the top. If you have experience with AutoCAD, this is easier to understand. The graphs we see are actually stacked layers of graphics that are completely invisible, or partially visible, or completely visible. So, surface can understand this: it is an area of memory, it is surfaceview visible not that part, the drawing operation acts on it, then it will be graphics card and other display controller drawn to the screen. surface is a, probably already have some concept. Because it corresponds to a memory area, we all know that the memory area of the object is a life cycle, can be dynamically requested to create and destroy, of course, may also be updated. So, there is the operation that works on this memory area, these operations are surfacecreated/changed/destroyed. Three operations are put together, that is callback, so in many cases see, there will be callback. callback, is a callback, meaning that they can do some work, but they do not take the initiative to do, but to other people to register, when others need, will ask me to do. For this example, a construction worker team A can build a house, decorate, or demolish a house. But he does not take the initiative to do these things, but to the developer B to register these three kinds of abilities, of course, he put these three kinds of ability to pack, called a ability. When B has a job, it is called A to do, or to build a house, or to decorate, or to demolish a building. In this case, the capacity package is callback. said this example, in fact, explains some of the surface related things, callback has said, below to say other. Let's say that surface is a house, so SuRface owns Surfaceholder, who is it? In this case, it's like building team A. The house corresponds to the surfacecreated, demolition of the house corresponds to the surfacedestroyed, decoration on the corresponding surfacechanged. surface has a survival period, like the existence of the house, after the construction of the existence, After the demolition, there was no more. The decoration must take place between this. Similarly, the change of surface must occur between created and destroyed. surfaceview knows who the holder of surface is, and when Surfaceview is generated, it calls Getholder to get holder, The holder then calls Addcallback to register the three callback functions. holder has control over your surface. In many programs, another thread is created in the surfacecreated function implementation. So there are two threads here, one is the UI thread and the other is the thread responsible for drawing. The drawing thread is created when the UI thread calls Surfacecreated, and is placed back into the thread pool when the surfacedestroyed call is made. In this middle, the drawing thread is responsible for drawing the graph. In this model, the UI thread and the drawing thread are responsible for interacting with the user, the latter being responsible for drawing the graph. This way, if the graph is drawn for a longer time, the user's input will not be blocked. We know that threads share memory data, so surface is shared with two threads. So, to avoid drawing, the UI thread also operates on surface, which requires you to lock your surface before drawing. This job is holder dry, holder will lock the surface of a holder.lockcanvas, we call canvas, then, painting on it, after the painting, will unlock unlockcanvasandpost. in applications, paint can be a one-time or timed drawing triggered by a timer. The Run method in the Runnable class is implemented. With regard to the Runnable class, which is defined in Java, is not unique to Android, the most important benefit of referring to Java's referrence. model is that paint does not depend on the UI thread and does not block the UI thread. The simple view is dependent on the UI thread for drawing. For updates that depend entirely on the user's input for image display, it is possible to use view, but if you can draw automatically without waiting for the user's input, surfAceview is undoubtedly a better choice. There is a picture of the truth, please look at the picture