When we need to develop a complex game, and the execution efficiency of the program is very high, the view class can not meet the requirements, then must be developed with the Surfaceview class.
For example, when it comes to a game with a high speed, the view class does not meet the requirements, and it must be developed using the Surfaceview class. For example, a game that requires a high speed can be displayed with double buffering. The background, characters, animations and so on in the game need to be drawn on a canvas (Canvas), while the Surfaceview can access a canvas directly, Surfaceview is provided for applications that need a direct portrait element instead of using a form part. Each surface creates a canvas object (but often changes the properties) to manage the drawing operations on view and surface.
Specific examples:
Activity
public class Activity01 extends activity {
Gamesurfaceview gameview = null;
@Override public
void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Gameview = new Gamesurfaceview (this);
Setcontentview (Gameview);
}
Gamesurfaceview
public class Gamesurfaceview extends Surfaceview implements Surfaceholder.callback, runnable{privat
e Activity01 mactivity = null;
Private Surfaceholder mholder = null;
Private Thread mthread = null;;
Private Boolean mloop = false;
Private Canvas Mcanvas = null;
Private Paint mpaint = null;
Private Long Drawstarttime = 0, drawcosttime = 0, drawtotaltime = 30;
private float x, y;
private int screenwidth = ScreenHeight = 800;
Public Gamesurfaceview {Super (context);
Mactivity = (Activity01) context;
Mholder = This.getholder ();
Mholder.addcallback (this);
Setfocusable (TRUE);
@Override public void surfacecreated (Surfaceholder holder) {mpaint = new Paint ();
Mpaint.setcolor (Color.cyan); Mloop = true;
New Thread (This). Start ();
@Override public void surfacechanged (surfaceholder holder, int format, int width, int height) {
ScreenWidth = width;
ScreenHeight = height;
x = SCREENWIDTH/2;
y = SCREENHEIGHT/2;
@Override public void surfacedestroyed (Surfaceholder holder) {Mloop = false; @Override public void Run () {while (mloop) {drawstarttime = Systemclock.upt
Imemillis ();
Synchronized (Mholder) {try {Mcanvas = Mholder.lockcanvas (); OnDraw ();
Repaint} catch (Exception e) {e.printstacktrace (); finally {if (Mcanvas!= null) {MholdEr.unlockcanvasandpost (Mcanvas);
}} drawcosttime = Systemclock.uptimemillis ()-drawstarttime;
LOG.I ("Drawcosttime", "drawcosttime =" + Drawcosttime); try {if (Drawcosttime < Drawtotaltime) {Thread.Sleep (Drawtotaltime-draw Costtime); Sleep elapse Time} catch (Exception e) {E.printstacktrac
E ();
}} private void OnDraw () {if (Mholder = = NULL && Mpaint = null) {
Return
} Mpaint.setantialias (True);
Mpaint.setalpha (100); Mpaint.setcolor (Color.cyan);
Paint background Color mcanvas.drawrect (0, 0, screenwidth, ScreenHeight, Mpaint); Mpaint.setcolor (Color.Blue); Paint Foreground CoLor mcanvas.drawcircle (x, Y, M, mpaint);
@Override public boolean ontouchevent (Motionevent event) {super.ontouchevent (event);
Switch (event.getaction ()) {Case motionevent.action_down:this.x = Event.getx ();
This.y = Event.gety ();
Break
Case motionevent.action_move:this.x = Event.getx ();
This.y = Event.gety ();
Break
Case motionevent.action_up:this.x = Event.getx ();
This.y = Event.gety ();
Break
Default:this.x = Event.getx ();
This.y = Event.gety ();
Break return true; True then event can implemented again} @Override public boolean onKeyDown (int keycode, Keye Vent event) {SUPER.ONkeydown (KeyCode, event);
if (keycode = = Keyevent.action_down) {this.mActivity.finish ();
return true; }
}