The relationship between Surface,surfaceview,sufaceholder and surface clients in Android

Source: Internet
Author: User

This is where the camera camera feature is implemented to explain the relationship between Surface,surfaceview,sufaceholder and the surface client (the camera in this case), surface and its client (client, The link between the camera in this example is Surfaceholder, which controls the connection or disconnection between surface and its client, which is the camera in this example, and surfaceview the component at the level of the seat view hierarchy. You can think of it as hosting surface, and surface is the buffer that is actually used to receive the original pixel drawing of the image.

Surfacecreated is called when an attempted hierarchy containing Surfaceview is placed on the screen, and Surfaceholder is associated with surface and its client;

Calling the Surfacedestroyed method when Surfaceview is removed from the screen also means that surface is destroyed, Surfaceholder disconnecting surface and its client;

When surface first appears on the screen, call the Surfacechanged method, passing in parameters that tell the client the pixel format of surface and the width and height of the surface, telling the client how much of the drawing area is available.

Take a look at the example: (here just intercepts some of the code about the Surface,surfaceview,sufaceholder and the surface client (the camera in this case), The specific implementation code for the camera function (Camera.takepicture (camera.shuttercallback shutter,camera.picturecallback raw,camera.picturecallback JPEG) and its associated interface implementation) are not released, for the camera function implementation is interested in or have doubts about the pot friends can leave the mailbox, the code of Mcamera is a camera object.

1msurfaceview=(Surfaceview) View.findviewbyid (r.id.crime_camera_surface);2         //Surfaceholder is our link to the object of surface (the buffer area that actually hosts the image's original pixel)3         //In fact, Surfaceholder is a link between surface and the client of surface, because there's nothing to do to make sure that surface doesn't exist.4         //drawn, so a "middleman" (Surfaceholder) is needed to coordinate their relationship. When surface creation is complete, the Surfaceholder link client and5         //surface guarantees that they interact, and when surface is destroyed, Surfaceholder disconnects surface and its clients. 6Surfaceholder holder=Msurfaceview.getholder ();7 Holder.settype (surfaceholder.surface_type_push_buffers);8         //Surfaceholder.callback () to monitor the life cycle of your surface9Holder.addcallback (NewSurfaceholder.callback () {Ten             //calling this method when Surfaceview is removed from the screen also means that surface is destroyed and surfaceholder disconnects the surface and its clients.  One @Override A              Public voidsurfacedestroyed (Surfaceholder holder) { -                 //TODO auto-generated Method Stub -                 if(mcamera!=NULL){ the                     //surface is now destroyed, and the Stoppreview method stops the camera drawing operation on surface. - Mcamera.stoppreview (); -                 } -             } +             //surfacecreated is called when an attempted hierarchy containing Surfaceview is placed on the screen, and Surfaceholder is associated with surface and its clients - @Override +              Public voidsurfacecreated (Surfaceholder holder) { A                 //TODO auto-generated Method Stub at                 Try{ -                     if(mcamera!=NULL){ -                         //link camera and surface via Setpreviredisplay - Mcamera.setpreviewdisplay (holder); -                     } -}Catch(IOException e) { inLOG.E (TAG, "Setting up preview display failed", e); -                 } to             } +             //surface first appears on the screen call this method, through the parameters passed in, you can tell the client surface of the pixel format and surface width and height, -             //tells the client how large the drawing area can be used.  the @Override *              Public voidSurfacechanged (Surfaceholder holder,intFormatintWidthintheight) { $                 //TODO auto-generated Method StubPanax Notoginseng                 if(mcamera==NULL){ -                     return; the                 } +Camera.parameters parameters=mcamera.getparameters (); ASize size=getbestsupportedsize (parameters.getsupportedpreviewsizes (), width, height); the parameters.setpreviewsize (Size.width, size.height); +Size=getbestsupportedsize (parameters.getsupportedpicturesizes (), width, height); - parameters.setpicturesize (Size.width, size.height); $ mcamera.setparameters (parameters); $                 Try{ -                     //tell by Startpreview how you can draw on your surface - Mcamera.startpreview (); the}Catch(Exception e) { -LOG.E (TAG, "Start preview failed", e);Wuyi mcamera.release (); theMcamera=NULL; -                 } Wu             } -});

The relationship between Surface,surfaceview,sufaceholder and surface clients in Android

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.