First, what is surface
Simply put, surface corresponds to a screen buffer, and each window corresponds to a surface, and any view is drawn on the canvas of your surface (for explanation later). The traditional view shares a piece of screen buffer, and all the drawing must be done in the UI thread.
In the documentation for the SDK, the description of surface is this: "Handle onto a raw buffer that's being managed by the screen compositor", translated into Chinese is "display content synthesizer (scre En compositor) manages the handle of the original buffer ", this sentence includes the following two meanings:
1. With surface (because surface is a handle) you can get the native buffer and its contents. Just as in the C + + language, you can get the contents of a file by using a handle to the file.
2, the original buffer (a raw buffers) is used to save the current window of the pixel data.
On the ground, it can be thought that surface in Android is a place for drawing graphics or images (image).
Based on the general knowledge of Java, we know that drawing is usually done on a canvas object, so you can infer that a surface object should contain a canvas object. Thus, on the basis of the two meanings mentioned above, one can be added:
3. There is a canvas member in surface that is dedicated to drawing.
From the above summary, we summarize as follows: The canvas member in surface is a place for programmers to paint, like a blackboard, where the original buffer is where the data is stored; surface itself acts like a handle, With this handle, you get the canvas, the original buffer, and everything else.
Surface is used to manage data. (handle)
Second, what is Surfaceview
Said Surfaceview is a View may not be rigorous, but from the definition of PUBILC Classsurfaceview extends view{...} Show Surfaceview is really derived from view, but Surfaceview has its own surface, see Surfaceview Source:
-
<span style= "Font-size:14px;color: #333333;" > if (Mwindow = = null = new Mywindow (this); Mlayout.type = Mwindowtype; Mlayout.gravity = gravity.left| gravity.top; Msession.addwithoutinputchannel (Mwindow, Mwindow.mseq, mlayout, mvisible ? Visible:gone, mcontentinsets); } </span>
It is clear that each Surfaceview created will create a mywindow,new Mywindow (this) in this is the surfaceview itself, so the surfaceview and window are bound together, By the first part we know that a window corresponds to a surface, so Surfaceview also has a built-in surface that can be thought of as surfaceview to control the location and size of the view in your surface.
Surfaceview is where you show data in your surface, and you can think of Surfaceview as a way to control the location and size of the view in your surface.
As you all know, traditional view and its derived classes are updated only on the UI thread, but the UI thread also handles other interaction logic, which does not guarantee the speed and frame rate of the view update, and Surfaceview can be drawn with a separate thread, thus providing a higher frame rate, such as a game, Camera framing and other scenes are more suitable for surfaceview.
Third, what is Surfaceholder
Surfaceholder is an interface that acts like a listener on surface, providing access to and control over surfaceview embedded surface-related methods. It allows us to perceive the creation, destruction, or alteration of surface by three callback methods.
There is a method Getholder in Surfaceview that makes it easy to get the listener interface Surfaceholder for Surfaceview's embedded surface (a bit of a mouthful).
In addition to the surfaceholder.callback mentioned below, Surfaceholder also provides a number of important methods, the most important of which is:
1. Abstract void Addcallback (Surfaceholder.callbackcallback): Add a Surfaceholder.callback callback interface for Surfaceholder.
2. Abstract Canvas Lockcanvas (): Gets a canvas object and locks it. The resulting canvas object is actually a member of surface.
3, abstract Canvas Lockcanvas (Rect dirty): Ibid. However, it is more efficient to lock only the rectangular area specified by the dirty.
4. Abstract void Unlockcanvasandpost (canvas canvas): When you modify the data in your surface, release the sync lock, commit the changes, and then display the new data, while the related data in your surface is lost.
The purpose of the synchronization lock mechanism in 2, 3, and 4 is to make the data in surface not be changed during the drawing process. Lockcanvas is to prevent multiple threads from writing to the same canvas at the same time.
Summary: Surface, Surfaceview, and Surfaceholder are essentially well-known MVC, Model-view-controller, from the height of the design pattern. Models are the meaning of the model, or the data model, or, more simply, the data, which is where the Surface;view is the view, representing the user interface, which is where the Surfaceview The Surfaceholder is clearly understood to be the controller in MVC.
Iv. What is Surfaceholder.callback
Surfaceholder.callback is primarily a callback notification when the underlying surface is created, destroyed, or changed, because the drawing must be made before surface is created, Therefore surfacecreated and surfacedestroyed in Surfaceholder.callback are the boundaries of the drawing processing code.
Three interface methods are defined in Surfaceholder.callback:
1. Abstract void surfacechanged (surfaceholder holder, int format, int width, int height): When any structural changes occur on surface (format or size) , the method is called immediately.
2, abstract void surfacecreated (surfaceholder holder): When the Surface object is created, the method is called immediately.
3. Abstract void surfacedestroyed (surfaceholder holder): This method is called immediately before the surface object is about to be destroyed.
V. Demonstration of examples
Let's take a look at a very simple example, and note the comments in the code:
1. Create an Android Project Project Testsurfaceview in Eclipse and choose to generate the default activity testsurfaceviewactivity
2. Create a drawing thread as follows :
ImportAndroid.graphics.Canvas; ImportAndroid.graphics.Color; ImportAndroid.graphics.Paint; ImportAndroid.graphics.Rect; Importandroid.view.SurfaceHolder; //Drawing Threads Public classMyThreadextendsThread {PrivateSurfaceholder Holder; Private Booleanrun; PublicMyThread (Surfaceholder holder) { This. Holder =Holder; Run=true; } @Override Public voidrun () {intCounter = 0; Canvas Canvas=NULL; while(run) {//Specific drawing work Try { //gets the canvas object and locks thecanvas=Holder.lockcanvas (); //set the background color of a canvas objectCanvas.drawcolor (Color.White); //Create a brushPaint p =NewPaint (); //Set Brush ColorP.setcolor (Color.Black); //Set Text SizeP.settextsize (30); //creates a Rect object rectRect rect =NewRect (100, 50, 380, 330); //draw a rect on the canvasCanvas.drawrect (rect,p); //Show time on canvasCanvas.drawtext ("Interval =" + (counter++) + "seconds.", 100, 410, p); Thread.Sleep (1000); } Catch(Exception e) {e.printstacktrace (); } finally { if(Canvas! =NULL) { //unlock and commit the changesholder.unlockcanvasandpost (canvas); } } } } Public BooleanIsrun () {returnrun; } Public voidSetrun (Booleanrun) { This. Run =run; } }
3. Customize a Surfaceview class as follows:
1 ImportAndroid.content.Context; 2 ImportAndroid.view.SurfaceHolder; 3 ImportAndroid.view.SurfaceView; 4 5 Public classMysurfaceviewextendsSurfaceview<span style= "font-family:arial;" > </span>Implements<span style= "font-family:arial;" > </span>Surfaceholder.callback6 { 7 PrivateSurfaceholder Holder; 8 PrivateMyThread MyThread; 9 Ten PublicMysurfaceview (Context context) One { A Super(context); - - //get Surfaceholder objects with Surfaceview theHolder =Getholder (); - - //Add a callback structure for holder Surfaceholder.callback -Holder.addcallback ( This); + - //create a drawing thread and pass the holder object as a parameter so that you can get holder in the drawing thread + //object, and in the drawing thread, you can get the canvas object from the holder object and draw it on the canvas AMyThread =NewMyThread (holder); at } - - //implementing the three methods in the Surfaceholder.callback interface is called in the main thread, not in the drawing thread . - @Override - Public voidSurfacechanged (Surfaceholder holder,intFormatintWidthintheight) - { in } - to @Override + Public voidsurfacecreated (surfaceholder holder) - { the //start the thread. When this method is called, it means that surface is already working. *Mythread.setrun (true); $ Mythread.start (); Panax Notoginseng } - the @Override + Public voidsurfacedestroyed (surfaceholder holder) A { the //ends the thread. When this method is called, it means that surface is going to be destroyed. +Mythread.setrun (false); - } $}
4. Modify the Testsurfaceviewactivity.java code to make it as follows:
1 Import android.app.Activity;
2 ImportAndroid.os.Bundle; 3 4 Public classTestsurfaceviewactivityextendsActivity5 { 6 @Override7 Public voidonCreate (Bundle savedinstancestate)8 { 9 Super. OnCreate (savedinstancestate); Ten //Setcontentview (R.layout.main); OneSetcontentview (NewMysurfaceview ( This)); A } -}
Operation Result:
Original: http://blog.csdn.net/tgww88/article/details/7973476
Surface and Surfaceview in Android