Android Surfaceview Detailed

Source: Internet
Author: User

Weekend Watch "proficient in Android game development (Pro Android games), the inside of 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 is the picture on the screen, which 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 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.  In this way, surface can understand this: it is an area of memory, it is surfaceview visible not that part, the drawing operation action on it, then it will be graphics card and other display controller drawn to the screen.  What surface is, probably already has 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 put together, is callback,So in many cases, 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 pack is callback. This example, in fact, explains some of the things that are related to surface, callback has already said, and the rest. If surface is a house, then surface has 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 lifetime, like the existence of a house, after the construction of the existence, after the demolition has not. 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, Getholder gets holder when Surfaceview is generated, and holder calls Addcallback to register 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 the application, paint can be a one-time or timed drawing triggered by a timer. The Run method in the Runnable class is implemented. About the Runnable class, defined in this Java, is not unique to Android, you can refer to the Java referrence. 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 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 Surfaceview is undoubtedly a better choice if it is possible to draw automatically without waiting for the user's input.  There is a picture of the truth, please look at the picture

Android Surfaceview Detailed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.