Surfaceview is inherited from the View class, has the function of drawing the dynamic picture, playing the video. View is responsible for updating the animation in the main thread, while Surfaceview is updating the animation in a new one, and it is a 2D canvas effect. Put yourself on the lowest level of the screen to display the picture,
How to use Surfaceview:
1. Span style= "FONT-SIZE:19PX; Font-family: Song Body "" When creating a surfaceview Project
The project needs to inherit the Surfaceview and interface to the surfaceholder Callback and Runnable which surfaceholder is used to monitor the canvas,Runnable is used to open a new process, Draw the picture in the process.
Public classGameviewokextendsSurfaceviewImplementsCallback, Runnable
when inheritance and interfaces are complete, there are three constructors and an overloaded function, respectively:surfacecreated,surfacechanged,surfacedestroyedas wellGameviewok(example), three of which Constructionfunctionrespectively indicateSurfaceview in the creation, alteration and destruction of the relevant processing to be done, the general drawing is placed in thesurfacechangedin the. Using code in overloaded functions
Surfaceholder = This . Getholder ();
Surfaceholder . Addcallback (this); //Registration callback method
To set Surfaceholer and add callback methods, do not add this callback will report null pointer exception,surfaceholder equivalent to the remote control Surfaceview.
Surfaceview is mainly used to draw pictures, through the thread to achieve the animation effect of the picture, in the process of drawing a picture, it takes three steps:
(1) get the canvas and lock the canvas
Canves = Surfaceholder.lockcanvas()
(2) then do the painting in the canvas
(3) when you are finished drawing, use the Surfaceholder.unlockcanvas method to unlock the canvas for subsequent operations.
2. mianactivity add surfaceview
also first set up a java surfaceview surfaceholder.callback,runnable mainactivity setcontext
Setcontentview (Gameviewok);
3. Adding controls on Surfaceview
When you need to add a control on Surfaceview, simply add the class that inherits from Surfaceview to the layout file as a control.
<controltool.surfaceview
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"/>
This allows you to add additional controls on the Surfaceview.
Surfaceview Controls in Android