[Book Note: Android game programming starts from scratch] 10. Game Development BASICS (View game framework), Learning android from scratch

Source: Internet
Author: User

[Book Note: Android game programming starts from scratch] 10. Game Development BASICS (View game framework), Learning android from scratch

For players, the game is dynamic; for game developers, the game is static, but the video cannot be played continuously, so that players can see the dynamic effect.

Before entering Android, you must first familiarize yourself with three important classes: View, Canvas, and Paint ). Through the paint brush, you can draw a variety of wonderful images and images on the canvas, and then display the content on the canvas on the mobile phone screen through the view.

Secondly, you should be familiar with the concept of "screen Flushing. Images drawn on the canvas, whether they are images or images, are static. dynamic effects can be achieved only by constantly displaying different canvases. On a mobile phone, the canvas is always one, so it is impossible to continuously play different canvases to achieve dynamic effects. In this case, you need to refresh the canvas to achieve dynamic effects.

Refreshing the canvas is like using an eraser to erase all the content on the canvas, and then re-draw the canvas. This is repeated to form a dynamic effect. The process of wiping the canvas is called screen flushing (screen refreshing ).

Three commonly used views in Android game development are View, SurfaceView, and GLSurfaceView. The following describes the meanings of the three views:

View: display View, built-in canvas, drawing function, touch screen event, button event function, etc;
SurfaceView: Expanded View Class Based on View, more suitable for 2D game development;
GLSurfaceView: Expanded View Class Based on SurfaceView view, dedicated to 3D Game Development View.

View game framework

1. drawing function onDraw
Create a new project named GameView. After the project is created, define a View class "MyView" to inherit the View class. The Code is as follows:

Package com. example. ex4_4; import android. content. context; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. view. keyEvent; import android. view. motionEvent; import android. view. view; public class MyView extends View {private int textX = 20, textY = 20;/*** rewrite the parent class constructor * @ param context */public MyView (Context context) {super (context); // set the focus setFocusab Le (true);}/***** rewrite the event by pressing the key * @ param keyCode the current user clicked button * @ param event Action event queue, this class also defines many static constant key values */@ Override public boolean onKeyDown (int keyCode, KeyEvent event) {// determines whether the user presses the key value for the "up/down" key if (keyCode = KeyEvent. KEYCODE_DPAD_UP) {// click the "on" button to make the Y coordinate of the text smaller textY-= 2;} else if (keyCode = KeyEvent. KEYCODE_DPAD_DOWN) {// "press the" button to enlarge the Y coordinate of the text. textY + = 2;} else if (keyCode = KeyEvent. KEYCODE_DPAD_LEFT) {// "Left" button clicked To reduce the X coordinate of the text. textX-= 2;} else if (keyCode = KeyEvent. KEYCODE_DPAD_RIGHT) {// click the "right" button to enlarge the X coordinate of the text. textX + = 2;} return super. onKeyDown (keyCode, event);}/*** rewrite the key to raise the event */@ Override public boolean onKeyUp (int keyCode, KeyEvent event) {// invalidate (); you cannot execute it cyclically in the current Child thread // postInvalidate (); you can execute invalidate () cyclically in the Child thread; // re-draw the canvas return super. onKeyUp (keyCode, event);}/*** rewrite the touch screen event function */@ Override public boolea N onTouchEvent (MotionEvent event) {// obtain the X coordinate assignment of the user's finger touch screen and the X coordinate int x = (int) event of the text. getX (); // obtain the Y coordinate assignment of the user's finger touch screen and the Y coordinate int y = (int) event of the text. getY (); textX = x; textY = y; // re-paint cloth invalidate (); return true ;} /*** Override the parent class plotting function */@ Override protected void onDraw (Canvas canvas) {// create a Paint brush instance paint = new Paint (); // set the paint color. setColor (Color. WHITE); // set the paint brush text size paint. setTextSize (18); // draw the text canvas. drawText ("Hi, hello! ", TextX, textY, paint); super. onDraw (canvas );}}

Modify the MainActivity class to display the rendered View.

public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(new MyView(this));}}

Modify the configuration file and set the application to full screen. Here, the topic is set to a black background and the status bar and application title are hidden.

android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" 

 

In fact, it is to inherit the View class and then override the method of the parent class.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.