The main addition to the control class in Android games is the Display Class View. Surfaceview is a display class that derives from the view base class. The three views commonly used in Android game development are: view, Surfaceview, and Glsurfaceview.
View: Display view, built-in canvas, provides graphical drawing functions, touch-screen events, key event functions, etc. , must be updated within the UI main thread, slower.
Surfaceview: Views based on the view of the expansion of the class, more suitable for 2D game development; is a subclass of view, similar to the use of double-slow mechanism to update the screen in a new thread so the refresh interface faster than the view.
Glsurfaceview: The view class, which is extended again based on the Surfaceview view, is designed for 3D game development, and is a subclass of Surfaceview, dedicated to OpenGL.
In the development of 2D game, it can be divided into two kinds of game frame, view and Surfaceview. View and Surfaceview differences:
View: The screen must be updated in the main thread of the UI for the passive update screen.
Both the Surfaceview:ui thread and the child thread are available. Redraw the screen in a newly started thread and actively update the screen.
Updating the screen in the main thread of the UI may cause problems, such as if you are updating the screen too long, then your main UI thread will be blocked by the function you are drawing. Then you will not be able to respond to keystrokes, touch screens and other messages. when using Surfaceview because it is updating the screen in a new thread, it does not block your UI main thread. But this also brings up another problem, that is, event synchronization, which involves thread synchronization.
Therefore, based on the above, according to the characteristics of the game, generally divided into two categories.
1 Passive Update screen. Like chess, this is good with view. Because the screen update is dependent on OnTouch to update, you can use the invalidate directly. Because of this, the touch and the next touch will take longer and will not have an impact.
2 active updates. Like a man running all the time. This requires a separate thread to redraw the human state continuously, avoiding blocking the main UI thread. So obviously the view is not suitable, need surfaceview to control.
Here is a basic framework for using Sufaceview:
Importandroid.app.Activity; ImportAndroid.content.Context; ImportAndroid.graphics.Canvas; ImportAndroid.graphics.Color; ImportAndroid.graphics.Paint; ImportAndroid.os.Bundle; ImportAndroid.view.SurfaceHolder; ImportAndroid.view.SurfaceView; Public classTestsurfaceviewextendsActivity {/**Called when the activity is first created.*/@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (NewMyView ( This)); } classMyViewextendsSurfaceviewImplementssurfaceholder.callback,runnable{Surfaceholder Holder=NULL; Paint paint; PublicMyView (Context context) {Super(context); //TODO auto-generated Constructor stubHolder=Getholder (); Holder.addcallback ( This); Paint=NewPaint (Paint.anti_alias_flag); Paint.setcolor (color.red); This. setfocusable (true); } @Override Public voidSurfacechanged (Surfaceholder holder,intFormatintwidth,intheight) { //TODO auto-generated Method Stub} @Override Public voidsurfacecreated (Surfaceholder holder) {//TODO auto-generated Method StubThread t=NewThread ( This); T.start (); } @Override Public voidsurfacedestroyed (Surfaceholder holder) {//TODO auto-generated Method Stubisrunning=false; } @Overrideprotected voidOnDraw (canvas canvas) {//TODO auto-generated Method Stubcanvas=Holder.lockcanvas (); //Brush ScreenCanvas.drawcolor (Color.Black); Canvas.drawcircle (x, Y,10, paint); Holder.unlockcanvasandpost (canvas); } Private voidPaint (Paint paint) {Canvas Canvas=Holder.lockcanvas (); //Brush ScreenCanvas.drawcolor (Color.Black); Canvas.drawcircle (x, Y,10, paint); Holder.unlockcanvasandpost (canvas); } Booleanisrunning=true; @Override Public voidrun () {//TODO auto-generated Method Stub while(isrunning) {//onDraw (NULL); paint (paint); Move (); Try{Thread.Sleep (50); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } } } Private intx, y; Private voidMove () {x+=2; Y+=2; } } }
Android View and Surfaceview