Surfaceview Game framework plays an important role in game development.
The Surfaceholder class is used to control the size and format of Surfaceview, and is primarily used to monitor the state of your surface.
Surfaceview seems to be a piece of memory data inside the record data, change the data is controlled by Surfaceholder, using Surfaceholder Lockcanvas () function to get the Surfaceview canvas object. Then draw the content on the canvas to modify the data in the Surfaceview.
ImportAndroid.content.Context;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Color;ImportAndroid.graphics.Paint;ImportAndroid.view.SurfaceHolder;ImportAndroid.view.SurfaceHolder.Callback;ImportAndroid.view.SurfaceView; Public classMysurfaceviewextendsSurfaceviewImplementsCallback {//This class is mainly used to monitor the state of Surfaceview. PrivateSurfaceholder Holder; PrivatePaint paint; PublicMysurfaceview (Context context) {Super(context); Holder= This. Getholder (); //Add listening status to your surfaceHolder.addcallback ( This); //instance a brushPaint =NewPaint (); //set the brush to whitePaint.setcolor (Color.White); } @Override Public voidSurfacechanged (Surfaceholder holder,intFormatintWidthintheight) {} @Override Public voidsurfacecreated (Surfaceholder holder) {Mydraw (); } @Override Public voidsurfacedestroyed (Surfaceholder arg0) {}Private voidMydraw () {//Lockcanvas not only to get canvas, but also to canvas locking//prevent damage during the drawing processCanvas Mcanvas =Holder.lockcanvas (); Mcanvas.drawtext ("Game", 50, 10, paint); Holder.unlockcanvasandpost (Mcanvas); } @Overrideprotected voidOnDraw (canvas canvas) {Canvas.drawtext ("Game", 50, 50, paint); Super. OnDraw (canvas); }}
The OnDraw () method, which is overloaded in Surfaceview, is not executed, and it is more illustrative that Surfaceview modifies the data through Surfaceholder, so Surfaceview will not execute even if the OnDraw () method is overridden. Next, repeat the last view operation, overriding the Ontouchevent () method.
@Override Public Boolean ontouchevent (Motionevent event) { = (int) event.getx (); = (int) event.gety (); Mydraw (); return true ;}
After discovering the rewrite Ontouchevent () method, the screen drawing becomes messy. After the last rewrite, the effect is completely different. The main reason for this is that the canvas is not refreshed, and the text that is drawn every time is displayed. The solution is to "brush the screen" of the canvas.
Private void Mydraw () { = Holder.lockcanvas (); // is to overwrite the original with a background before redrawing. Paint.setcolor (color.black); Mcanvas.drawrect (this. GetHeight (), paint); Paint.setcolor (color.white); Mcanvas.drawtext ("Game", PosX, PosY, paint); Holder.unlockcanvasandpost (Mcanvas);}
Surfaceview View Add thread
In the game, basically do not wait for the user to trigger an event to redraw the canvas, but a shorter fixed period to redraw the canvas, such as the dynamic flowers and grass, water and other scenes, these game elements do not interact with the player. However, these elements are dynamic. So in the game development, there will be a thread constantly to redraw.
ImportAndroid.content.Context;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Color;ImportAndroid.graphics.Paint;Importandroid.view.MotionEvent;ImportAndroid.view.SurfaceHolder;ImportAndroid.view.SurfaceHolder.Callback;ImportAndroid.view.SurfaceView; Public classMysurfaceviewextendsSurfaceviewImplementsCallback, runnable{//This class is mainly used to monitor the state of Surfaceview. PrivateSurfaceholder Holder; PrivatePaint paint; Private intPosX = 100; Private intPosY = 100; Private intFlag; PrivateCanvas Canvas; Private intScreenw, Screenh; PublicMysurfaceview (Context context) {Super(context); Holder= This. Getholder (); //Add listening status to your surfaceHolder.addcallback ( This); //instance a brushPaint =NewPaint (); //set the brush to whitePaint.setcolor (Color.White); Paint.settextsize (50f); //Set FocusSetfocusable (true); } @Override Public voidSurfacechanged (Surfaceholder holder,intFormatintWidthintheight) {} @Override Public voidsurfacecreated (Surfaceholder holder) {screenh= This. GetHeight (); Screenw= This. getwidth (); Flag= 1; NewThread ( This). Start (); } @Override Public voidsurfacedestroyed (Surfaceholder holder) {flag= 0; } Private voidMydraw () {Try{Canvas=Holder.lockcanvas (); if(canvas!=NULL){ //is to overwrite the original with a background before redrawing.Paint.setcolor (Color.Black); Canvas.drawrect (0, 0, This. GetWidth (), This. GetHeight (), paint); Paint.setcolor (Color.White); Canvas.drawtext ("Game", PosX, PosY, paint); } } Catch(Exception e) {}finally{ if(canvas!=NULL) {holder.unlockcanvasandpost (canvas); } }} @Override Public Booleanontouchevent (Motionevent event) {PosX= (int) Event.getx (); PosY= (int) event.gety (); Mydraw (); return true; } Private voidLogic () {}//Game Logic@Override Public voidrun () { while(flag==1){ LongStart =System.currenttimemillis (); Mydraw (); Logic (); LongEnd =System.currenttimemillis (); Try { //ensure that the minimum period for redrawing is if(End-start < 50) {Thread.Sleep (-(End-start)); } } Catch(interruptedexception e) { } } }}
The average game refresh time is 50-100 milliseconds, which is about 10-20 frames per second.
Finally thanks to Baidu, thanks to "game programming from the beginning of 0."
Android game Development (ii) a tentative study of Surfaceview