For players, the game is dynamic, for the game developers, the game is static, just keep playing the screen, so that the player can see the dynamic effect.
Before you enter Android, familiarize yourself with three important classes: view, canvas, paint (brush). With brushes, you can draw a variety of wonderful graphics, pictures, and so on on the canvas, and then view the contents of the canvas on your phone screen.
Second, be familiar with the concept of "brush screen". Images drawn in the canvas, whether pictures or graphics, are static, and only through the continuous display of different canvases to achieve dynamic results. On a mobile phone, the canvas is always just one, so it's not possible to make a dynamic effect by constantly playing different canvases, and then you need to refresh the canvas to achieve a dynamic effect.
Refreshing the canvas is like using a piece of eraser, erasing everything on the previous canvas, and then redrawing the canvas, so again and again, creating a dynamic effect, while the process of wiping the canvas is called a brush screen (refresh the screen).
The three views commonly used in Android game development are view, Surfaceview, and Glsurfaceview. Here's a brief description of what these three views mean:
View: Display view, built-in canvas, provide graphical drawing function, touch screen event, key event function, etc.
Surfaceview: The view class that expands based on view view, more suitable for 2D game development;
Glsurfaceview: A view class that is extended again based on the Surfaceview view, designed for use in 3D game development.
View Game Frame
1. Drawing function OnDraw
Create a new project Gameview, after creation, first customize a view class "MyView" inherits the View class, the code is as follows:
PackageCom.example.ex4_4;ImportAndroid.content.Context;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Color;ImportAndroid.graphics.Paint;Importandroid.view.KeyEvent;Importandroid.view.MotionEvent;ImportAndroid.view.View; Public classMyViewextendsView {Private intTextx=20,texty=20; /*** Override Parent class constructor *@paramContext*/ PublicMyView (Context context) {Super(context); //Set FocusSetfocusable (true); } /*** * Override Key Press event *@paramkeycode Current User-clicked button *@paramevent key, which also defines a number of static constant key values*/@Override Public BooleanOnKeyDown (intKeyCode, KeyEvent event) { //determines whether the key value pressed by the user is the "up or down" key of the arrow key if(keycode==keyevent.keycode_dpad_up) { //"Up" button is clicked, should make the text's Y coordinate smallertexty-=2; }Else if(keycode==Keyevent.keycode_dpad_down) { //the "Down" button is clicked and the y-coordinate of the text should be made largertexty+=2; }Else if(keycode==keyevent.keycode_dpad_left) { //"Left" button is clicked, should make the text's x coordinate smallertextx-=2; }Else if(keycode==keyevent.keycode_dpad_right) { //"Right" button is clicked, it should make the X coordinate of the text biggertextx+=2; } return Super. OnKeyDown (KeyCode, event); } /*** Override button lift Event*/@Override Public BooleanOnKeyUp (intKeyCode, KeyEvent event) { //invalidate (); Cannot loop call execution in current child thread//postinvalidate (); You can loop through a child thread to invoke executionInvalidate ();//Redraw the canvas return Super. OnKeyUp (KeyCode, event); } /*** Override touch-screen event functions*/@Override Public Booleanontouchevent (Motionevent event) {//gets the x-coordinate assignment of the user's finger touch screen and the text intx = (int) Event.getx (); //Gets the y coordinate of the user's finger touch screen and the text inty = (int) event.gety (); TEXTX=x; Texty=y; //Redraw the canvasinvalidate (); return true; } /*** Overriding the parent class drawing function*/@Overrideprotected voidOnDraw (canvas canvas) {//Create a Brush instancePaint paint =NewPaint (); //Set Brush ColorPaint.setcolor (Color.White); //Set Brush Text sizePaint.settextsize (18); //Draw TextCanvas.drawtext ("Hi, Hello!") ", TEXTX, texty, paint); Super. OnDraw (canvas); }}
Modify the Mainactivity class to display the drawing view
Public class extends Activity {@Overrideprotectedvoid onCreate (Bundle savedinstancestate) { Super . OnCreate (savedinstancestate); Setcontentview (new MyView (this));}}
Modify the configuration file, set the application to full screen, set the theme to black background and hide the status bar and app title
is actually inheriting the view class, and then overriding the parent class's methods.
"The zero start of Android game Programming" 10. Basics of game development (View game framework)