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. In the SDK documentation, the surface is described as follows: "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 includes the following two meanings:

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

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

Extended, we can think that the surface in Android is a place used to draw graphics or images. According to the general knowledge of Java, we know that drawing is usually performed on a canvas object. Therefore, we can infer that a surface object should contain a canvas object. In fact, this is true, in addition, this can be easily proved by running the program in debug mode (by moving the cursor over the object variable surface, 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 also prove this:


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

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

Therefore, the canvas member in the surface is a place for programmers to draw pictures, just like a blackboard. The native buffer is used to save data. The surface itself acts like a handle, with this handle, you can get the canvas, native buffer, and other content.

 

Ii. surfaceview

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


That is to say, the surface can be displayed through surfaceview. 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 usually only use five of these fields, so we can create a view containing only the five fields on the basis of the original data table through the SQL statement createview.

Surfaceview is a subclass of View in Android. In fact, in Android, all the classes used for interface display are view subclasses, 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 surface-related methods (providingaccess and control over this surfaceview's underlying surface) for accessing and controlling surfaceview. 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 (a bit of an interface ).

 

In addition to surfaceholder. Callback, surfaceholder also provides many 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 ()

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. However, it is more efficient to lock only the rectangle area specified by dirty.

4. Abstract void unlockcanvasandpost (canvascanvas)

After the data in the surface is modified, 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 parameters:

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 surface data 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, the image preview will be smoother. If this type is set, you cannot call lockcanvas to obtain the canvas object. Note that in the android SDK of the higher 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. by returning to the method, we can perceive the creation, destruction, or change of the surface. 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 is called immediately when the surface structure changes (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 documentation includes 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:

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 this method, pay attention to some thread semantics:

-All methods declared in surfaceview and surfaceholder. Callback must be called in the thread running the surfaceview window (typically, the main thread of the application. (Ui threads), because they need to correctly synchronize various states simultaneously accessed by the drawing thread.

-Ensure that only the surface on the back is valid-access the surfaceholder. Callback. surfacecreated () and surfaceholder. Callback. surfacedestroyed () methods.

 

Next, let's take a very simple example (the code is taken from annotations). Pay attention to the comments in the Code:

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

2. Create a drawing thread as follows:

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) {// specific drawing work try {// get the canvas object and lock the 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 modification content holder. unlockcanvasandpost (canvas) ;}}} public Boolean isrun () {return run;} public void setrun (Boolean run) {This. run = run ;}}


3. Customize 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 the callback structure surfac for the holder Eholder. callback holder. addcallback (this); // create a drawing thread and pass in the holder object as a parameter. In this way, the holder // object can be obtained in the drawing thread, then, the canvas object can be obtained through the holder object in the painting 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 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));    }}

 

Running result:

 

Obviously, we can do a lot of more 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.

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.