@author: Qingdujun
The use of Surfaceview is very simple, as long as the implementation of Surfaceview and implementation of callback interface can be;
PackageCom.qdj.surfaceviewone;ImportCom.qdj.ui.SurfaceViewOne;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.view.Window;ImportAndroid.view.WindowManager;ImportAndroid.os.Bundle; Public classMainactivityextendsactionbaractivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); /*Set Window*/GetWindow (). SetFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FL Ag_fullscreen); Requestwindowfeature (Window.feature_no_title); /*define view by yourself*/Setcontentview (NewSurfaceviewone ( This)); }}
Custom Surfaceview
PackageCom.qdj.ui;ImportAndroid.content.Context;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Color;ImportAndroid.graphics.Paint;ImportAndroid.view.SurfaceHolder;ImportAndroid.view.SurfaceHolder.Callback;ImportAndroid.view.SurfaceView; Public classSurfaceviewoneextendsSurfaceviewImplementsCallback, Runnable {PrivateSurfaceholder Sfholder; Privatethread thread; PrivateCanvas Canvas; PrivatePaint paint; intScreenw, Screenh; intclock; PublicSurfaceviewone (Context context) {Super(context); Clock= 1; Thread=NewThread ( This); Sfholder=Getholder (); Sfholder.addcallback ( This); Paint=NewPaint (); Paint.setantialias (true); Paint.setcolor (Color.Blue); Setkeepscreenon (true); } Private voidDraw () {Try { /*get a Canvas object.*/Canvas=Sfholder.lockcanvas (); /*set BG color.*/Canvas.drawcolor (Color.White); Canvas.drawtext ("This is a game." + (clock++), 100, 100, paint); } Catch(Exception e) {}finally { /*Finish editing pixels in the surface.*/ if(Canvas! =NULL) {sfholder.unlockcanvasandpost (canvas); } }} @Override Public voidrun () { while(true) {draw (); Try { /*delay 100ms to let us can see.*/Thread.Sleep (100); } Catch(Exception e) {//Todo:handle Exception} }} @Override Public voidsurfacecreated (Surfaceholder holder) {/*get screen size.*/Screenw=getwidth (); Screenh=getheight (); /*start the thread.*/Thread.Start (); } @Override Public voidSurfacechanged (Surfaceholder holder,intFormatintwidth,intheight) { //TODO auto-generated Method Stub} @Override Public voidsurfacedestroyed (Surfaceholder holder) {//TODO auto-generated Method Stub }}
Android game Development (ii): Use of Surfaceview