Android Surfaceview Learning (i)
First, let's take a look at the official API's introduction to Surfaceview.
Surfaceview's API Introduction
Provides a dedicated drawing surface embedded inside of a view hierarchy. You can control the format of this surface and, if you like, its size; The Surfaceview takes care of placing the surface at the correct
The surface is Z, ordered so, it is behind, the window holding its surfaceview; The Surfaceview punches a hole in it window to allow it surface to be displayed. The view hierarchy'll take care of correctly compositing with the Surface any siblings of the surfaceview that would nor Mally appear on top of it. This can is used to place overlays such as buttons on top of the Surface, though note however that it can has an impact O n performance since a full alpha-blended composite'll be performed each time the Surface changes.
Access to the underlying surface was provided via the Surfaceholder interface, which can retrieved by calling Getholder ( ).
The Surface would be a created for your while the Surfaceview's window is visible; You should implement surfacecreated (Surfaceholder) and surfacedestroyed (Surfaceholder) to discover when the Surface is CRE Ated and destroyed as the window is shown and hidden.
One of the purposes of this class was to provide a surface in which a secondary thread can render in to the screen. If you is going to use the IT this is, you need to be aware of some threading semantics:
all Surfaceview and Surfaceholder.callback methods would be called from the thread running the Surfaceview ' s window (typic Ally the main thread of the application). They thus need to correctly synchronize with any state that's also touched by the drawing thread.
you must ensure that the drawing thread only touches the underlying Surface while it's valid--between surfaceholder.ca Llback.surfacecreated () and SurfaceHolder.Callback.surfaceDestroyed ().
The corresponding Chinese translation
Surfaceview is the inheritance class for view, which has a built-in surface for drawing. You can control the format and size of this surface. Surfaceview controls where this surface is drawn.
Surface is a deep sort (z-ordered), which indicates that it is always behind its own window. The Surfaceview provides a visible area that is visible only on the surface portion of the visible area and not visible outside the visible area. The layout display of surface is affected by the view hierarchy, and its sibling view nodes are displayed at the top. This means that the content of surface is obscured by its sibling view, which can be used to place a cloak (overlays) (for example, text and buttons). Note that if you have transparent controls on your surface, each change will cause the framework to recalculate the transparency of its and top-level controls, which can affect performance.
You can get this interface by accessing the Surface,getholder () method through the Surfaceholder interface.
Surface is created when Surfaceview becomes visible, and surface is destroyed before Surfaceview is hidden. This will save resources. If you want to see when your surface is created and destroyed, you can reload surfacecreated (Surfaceholder) and surfacedestroyed (Surfaceholder).
The core of Surfaceview is the provision of two threads: the UI thread and the render thread. It should be noted here:
1> all methods of Surfaceview and Surfaceholder.callback should be called in the UI thread, which is generally the main thread of the application. The various variables to be accessed by the render thread should be processed synchronously.
2> because surface may be destroyed, it is only valid between SurfaceHolder.Callback.surfaceCreated () and SurfaceHolder.Callback.surfaceDestroyed (), So make sure that the rendering thread is accessing the legitimate, effective surface.
Next, talk about your understanding of it.
1. Definition
Image data can be obtained directly from hardware interfaces such as memory or DMA, which is a very important drawing container.
It is characterized by the ability to draw on the screen in threads other than the main thread. This prevents the main thread from being blocked when the drawing task is heavy, which improves the program's response speed. In the game development more use Surfaceview, the game background, the character, the animation and so on as far as possible to draw in canvas canvases.
2. Realize
First inherit Surfaceview and implement Surfaceholder.callback interface
Reasons for using interfaces: because there is a principle of using Surfaceview, all the drawing work must be done before surface is created (surface-surface, which is often referred to in graphical programming.) Basically we can think of it as a mapping of video memory, to the content of surface
Can be copied directly to the video memory to display it, which makes the display very fast, and must end before surface is destroyed. So the surfacecreated and surfacedestroyed in callback are the boundaries of the drawing processing code.
Methods that need to be overridden
(1) public void surfacechanged (Surfaceholder holder,int format,int width,int height) {}
Fires when the size of your surface changes
(2) public void surfacecreated (Surfaceholder holder) {}
Fired at creation time, typically where the drawing thread is called.
(3) public void surfacedestroyed (Surfaceholder holder) {}
Fires when destroyed, typically where the drawing thread is stopped and released.
The whole process: inherit Surfaceview and implement Surfaceholder.callback interface----> Surfaceview.getholder () Get Surfaceholder object----> Surfaceholder.addcallback (callback) Add a callback function---->surfaceholder.lockcanvas () Get the canvas object and lock the canvas----> Canvas painting---- >surfaceholder.unlockcanvasandpost (canvas canvas) ends the lock drawing and commits the change to display the graphic.
3, Surfaceholder
Here's a class surfaceholder that you can use as a controller for your surface to manipulate your surface. Handle it on canvas to draw effects and animations, control surfaces, sizes, pixels, etc.
Several methods to be aware of:
(1), abstract void Addcallback (Surfaceholder.callback Callback);
Give Surfaceview the current holder a callback object.
(2), abstract Canvas Lockcanvas ();
Lock the canvas, usually after it is locked, it can be drawn through canvas objects it returns, drawing on it, and so on.
(3), abstract Canvas Lockcanvas (Rect dirty);
Lock an area of the canvas for paint, etc... Since the drawing is finished, the following unlockcanvasandpost will be called to change the display.
Compared to some of the higher memory requirements of the game, you can not redraw the dirty outside the pixels of other areas, can improve speed.
(4), abstract void unlockcanvasandpost (canvas canvas);
End the lock drawing and commit the change.
4. Example
The example here implements a rectangle and a timer
1 package xl.test;
2
3 Import android.app.Activity;
4 Import Android.content.Context;
5 Import Android.graphics.Canvas;
6 Import Android.graphics.Color;
7 Import Android.graphics.Paint;
8 Import Android.graphics.Rect;
9 Import Android.os.Bundle;
Ten import Android.view.SurfaceHolder;
Import Android.view.SurfaceView;
12
public class Viewtest extends Activity {
/** called when the activity is first created. */
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (New MyView (this));
19}
20//View inner class
Class MyView extends Surfaceview implements Surfaceholder.callback
22 {
Surfaceholder Holder;
Private MyThread MyThread;
Public MyView (Context context) {
+ super (context);
auto-generated//TODO constructor stub
Holder = This.getholder ();
Holder.addcallback (this);
MyThread = new MyThread (holder);//Create a drawing thread
31}
32
@Override
public void surfacechanged (surfaceholder holder, int format, int width,
int height) {
//TODO auto-generated method stub
37
38}
39
@Override
surfacecreated public void (Surfaceholder holder) {
/TODO auto-generated Method stub
Mythread.isrun = true;
Mythread.start ();
45}
46
@Override
surfacedestroyed public void (Surfaceholder holder) {
/TODO auto-generated Method stub
Mythread.isrun = false;
51}
52
53}
54//Thread inner class
Class MyThread extends Thread
56 {
Holder private Surfaceholder;
Isrun public boolean;
MyThread Public (Surfaceholder holder)
60 {
This.holder =holder;
Isrun = true;
63}
@Override
+-public void run ()
66 {
int count = 0;
(Isrun)
69 {
The Canvas c = null;
Try
72 {
Synchronized (holder)
74 {
The C = Holder.lockcanvas ();//Lock the canvas, usually after it is locked, it can be drawn through the canvas object it returns, drawing on it, and so on.
C.drawcolor (Color.Black);//Set Canvas background color
p = new paint (); Create a brush
P.setcolor (Color.White);
Over rect r = new Rect (100, 50, 300, 250);
C.drawrect (R, p);
Bayi C.drawtext ("This is the first" + (count++) + "seconds", 310, p);
Thread.Sleep (1000);//Sleep time is 1 seconds
83}
84}
Exception (e) {
Todo:handle//exception
E.printstacktrace ();
88}
Finally
90 {
if (c!= null)
92 {
Holder.unlockcanvasandpost (c);//End lock drawing and commit the change.
94
95}
96}
97}
98}
99}
100}
Android Surfaceview Learning (a) turn around