Relationship between surface, surfaceview, surfaceholder, and surfaceholder. Callback

Source: Internet
Author: User

Reprinted please include URL: http://blog.csdn.net/pathuang68/article/details/7351317

1. Surface

Surface means "surface. This is the description of the surface in the SDK documentation: "handle onto a raw buffer that is being managed by the screen compositor ", translation into Chinese is "handle to the native buffer managed by screen compositor", which contains the following two meanings:

1. You can obtain the native buffer and content through the surface (because the surface is a handle. Just like in C, you can get the file content through a file handle;

2. rawbuffer is used to save the pixel data of the current form.

Extended, we can think that the surface in Android is a place used for drawing shapes or images. Based on the general knowledge of Java, we know that plotting is usually performed on a canvas object. Therefore, we can infer that a surface object should include a canvas object. In fact, this is true, this can be easily proved by running the program in debug mode (by moving the cursor over the surface of the object variable, a dialog box will pop up with the content in the red box, there is a compatilecanvas member variable on the surface) Of course, the source code can prove this:


Therefore, based on the two meanings mentioned above, we can add one more:

3. There is a canvas member in the surface for plotting.

Therefore, the canvas member in the surface is a place specifically used for drawing programmers, just like a blackboard; the native buffer in it is used to store data; the effect of surface itself is similar to that of a handle. With this handle, you can get the canvas, native buffer, and other content.

 

Ii. surfaceview

Surfaceview, as its name implies, is the surface view. With surfaceview, You can see part or all of the surface content. The following uses a picture to describe the relationship between the surface and surfaceview:


That is to say, the surface uses surfaceview to display the content. In terms of this meaning, the exact meaning of View in surfaceview should be viewport, that is, "the view". A friend who has done Database Design knows that a data table has 20 fields, however, we often only use five fields, so we can create a view that only contains the five fields on the basis of the original data table through the SQL statement createview.

On the other hand, surfaceview is a subclass of View in Android. In fact, all the classes used for interface display in Android are child classes of view, including invisible and various layout.

Therefore, the view in surfaceview has two meanings:

1. The meaning of viewport

2. surfaceview is the derived class of view.

 

In Android, the surface is derived from an object and implements the parcelable interface. When we see parcelable, We can naturally think of a Data container. surfaceview is used to display data in the surface. At this level, surface is the place where data is managed, and surfaceview is the place where data is displayed.

 

3. surfaceholder

Surfaceholder is an interface that acts like a surface listener. Provides methods (providingaccess and control over this surfaceview's underlying surface) for surface-related control. It uses three callback methods, this allows us to perceive the creation, destruction, or changes of the surface. There is a method getholder in surfaceview, which can easily obtain the surfaceholder corresponding to the surface of surfaceview ).

 

In addition to surfaceholder. Callback, surfaceholder also provides many important methods, the most important of which is:

1. Abstract void addcallback (surfaceholder. callbackcallback)

Join for surfaceholder? A surfaceholder. Callback callback interface.

2. Abstract canvas lockcanvas ()

Obtain and lock a canvas object. The canvas object obtained is actually a member of the surface.

3. Abstract canvas lockcanvas (rectdirty)

Same as above. But it only locks the rectangle area specified by dirty, so it is more efficient.

4. Abstract void unlockcanvasandpost (canvascanvas)

After the data in the surface is changed, release the synchronization lock, submit the changes, and then display the new data. At the same time, the data in the surface will be lost.

5. Public abstract void settype (INT type)

Set the surface type and receive the following number of workers:

Surface_type_normal: Use Ram to cache the normal surface of native data

Surface_type_hardware: Applicable to the DMA (Direct Memory Access) engine and hardware-accelerated Surface

Surface_type_gpu: GPU-accelerated Surface

Surface_type_push_buffers: indicates that the surface does not contain native data. The data used by the surface is provided by other objects. This type of surface is used in the camera image preview, and camera is responsible for previewing the surface data, in this way, image preview is smoother than preview. If this type is set, you cannot call lockcanvas to obtain the canvas object. Note that in the android SDK of the high version, the settype method has been depreciated.

The purpose of the synchronization lock mechanism in 2, 3, and 4 is to prevent the data in the surface from being changed during the painting process.


From the perspective of design patterns, surface, surfaceview, and surfaceholder are essentially well-known MVC, namely Model-View-controller. Model refers to the meaning of the model, or the data model, or more simply the data, that is, the surface here; view refers to the view, which represents the user interaction interface, that is, the surfaceview here; surfaceholder is clearly understood as the controller in MVC ). In this way, it seems that the relationship between the three is much clearer.

 

Iv. surfaceholder. Callback

As mentioned above, surfaceholder is an interface that allows us to perceive the creation, destruction, or change of the surface by returning to the method. In fact, this is achieved through its internal static sub-interface surfaceholder. callback. Surfaceholder. Callback defines three interface methods:

1. Abstract void surfacechanged (surfaceholderholder, int format, int width, int height)

This method will be called immediately when the surface changes regardless of the structure (format or size.

2. Abstract void surfacecreated (surfaceholderholder)

After a surface object is created, this method is called immediately.

3. Abstract void surfacedestroyed (surfaceholderholder)

This method is called immediately before the surface object is destroyed.

 

The description of surfaceview In the android SDK document contains the following:

One of the purposes of this class is to provide a surface in which a secondarythread can render into the screen. if you are going to use it this way, youneed to be aware of some threading semantics:

 

-All surfaceview and surfaceholder. callbackmethods will be called from the thread running the surfaceview's window (typically the main thread of the application ). they thus need to correctlysynchronize with any State that is also touched by the drawing thread.

-You must ensure that the drawingthread only touches the underlying surface while it is valid -- betweensurfaceholder. Callback. surfacecreated () andsurfaceholder. Callback. surfacedestroyed ().

This section is very important, which roughly means the following:

One of the purposes of this class is to provide a surface that can be rendered on the screen using another thread (the second thread ). If you want to use it like this, pay attention to the thread semantics:

-All methods declared in surfaceview and surfaceholder. Callback must be called in the thread in the surfaceview form (typically, the main thread of the application. Note: Ui threads), because they need to correctly synchronize various statuses that are asked by the plotting thread at the same time.

-It must be ensured that only the surface on the back is valid-when calling the surfaceholder. Callback. surfacecreated () and surfaceholder. Callback. surfacedestroyed () methods, the caller will ask it.

 

In the following example, we can use a very easy example (the code is taken from the example). Please pay attention to the gaze in the Code:

1. Create an android Project testsurfaceview in eclipse and select to generate the default activity testsurfaceviewactivity.

2. Create a drawing thread, for example, the following:

Package COM. pat. testsurfaceview; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. graphics. rect; importandroid. view. surfaceholder; // draw the thread public class mythread extendsthread {private surfaceholder holder; private Boolean run; Public mythread (surfaceholder holder) {This. holder = holder; run = true ;}@ override public void run () {int counter = 0; canvas = NULL; while (run) {// detailed drawing work try {// get the canvas object and lock it canvas = holder. lockcanvas (); // set the background color of the canvas object. drawcolor (color. white); // create a paint brush paintp = new paint (); // set the paint color p. setcolor (color. black); // set the text size P. settextsize (30); // create a rect object rect = new rect (100, 50,380,330); // draw a rect canvas on the canvas. drawrect (rect, P); // display the time canvas on the canvas. drawtext ("interval =" + (Co Unter ++) + "seconds. ", 100,410, P); thread. sleep (1000);} catch (exception e) {e. printstacktrace ();} finally {If (canvas! = NULL) {// unlock and submit the change content holder. unlockcanvasandpost (canvas) ;}}} public Boolean isrun () {return run;} public void setrun (Boolean run) {This. run = run ;}}

3. Define a surfaceview class as follows:

Package COM. pat. testsurfaceview; import android. content. context; import android. view. surfaceholder; import android. view. surfaceview; public class mysurfaceview extends surfaceviewimplementssurfaceholder. callback {private surfaceholder holder; private mythread; publicmysurfaceview (context) {super (context); // obtain the surfaceholder object holder = getholder () through surfaceview; // Add to Holder? Callback structure surfaceholder. callback holder. addcallback (this); // create a drawing thread and use the holder object as the handler to input data, so that the holder // object can be obtained in the drawing thread, then, the canvas object can be obtained through the holder object in the drawing thread, and mythread = new mythread (holder) is drawn on the canvas;} // surfaceholder is implemented. the three methods in the callback interface are called in the main thread, instead of the @ override public void surfacechanged (surfaceholder holder, int format, int width, int height) called in the drawing thread) {}@ override public void surfacecreated (surfaceholder holder) {// Start the thread. When this method is called, it indicates that the surface has valid mythread. setrun (true); mythread. Start () ;}@ override public void surfacedestroyed (surfaceholderholder) {// end the thread. When this method is called, it indicates that the surface is about to be destroyed mythread. setrun (false );}}
 

4. Modify the testsurfaceviewactivity. Java code to make it as follows:

package com.pat.testsurfaceview; import android.app.Activity;import android.os.Bundle; public class TestSurfaceViewActivity extends Activity{    @Override    public void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        //setContentView(R.layout.main);        setContentView(new MySurfaceView(this));    }}
 

Execution result:

 

Obviously, we can do a lot of interesting things in the mythread run method. Understanding the concepts of surface, surfaceview, surfaceholder, and surfaceholder. Callback, as well as their relationships, should be of great help for us to better use them.

Relationship between surface, surfaceview, surfaceholder, and surfaceholder. Callback

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.