Android game development View framework

Source: Internet
Author: User

Press the upper/lower key rectangle on the keyboard to move up and down;

The Handler object is instantiated and the handkeMessage method is rewritten to implement a message receiver. Then, the thread sends the update interface message through the sendMessage method. When the receiver receives the update interface message, it executes the invalidate method to update the screen display.

Package com. yarin. android. testOnView; import android. content. context; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. view. view; public class GameView extends View {int miCount = 0; int y = 0; public GameView (Context context) {super (context);} public void onDraw (Canvas canvas) {if (miCount <100) {miCount ++;} else {miCount = 0 ;}// drawing Paint mPaint = new Paint (); switch (miCount % 4) {case 0: mPaint. setColor (Color. BLUE); break; case 1: mPaint. setColor (Color. GREEN); break; case 2: mPaint. setColor (Color. RED); break; case 3: mPaint. setColor (Color. YELLOW); break; default: mPaint. setColor (Color. WHITE); break;} // draw a rectangular canvas. drawRect (320-80)/2, y, (320-80)/2 + 80, y + 40, mPaint) ;}} package com. yarin. android. testOnView; import android. content. context; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. view. view; public class GameView extends View {int miCount = 0; int y = 0; public GameView (Context context) {super (context);} public void onDraw (Canvas canvas) {if (miCount <100) {miCount ++;} else {miCount = 0 ;}// drawing Paint mPaint = new Paint (); switch (miCount % 4) {case 0: mPaint. setColor (Color. BLUE); break; case 1: mPaint. setColor (Color. GREEN); break; case 2: mPaint. setColor (Color. RED); break; case 3: mPaint. setColor (Color. YELLOW); break; default: mPaint. setColor (Color. WHITE); break;} // draw a rectangular canvas. drawRect (320-80)/2, y, (320-80)/2 + 80, y + 40, mPaint );}

} The above is used to draw the interface. We also need a class to control the operation of the application.


 

Package com. yarin. android. testOnView; import android. app. activity; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. view. keyEvent; import android. view. motionEvent; public class ControlView extends Activity {private static final int REFRESH = 0x000001; private GameView mGameView = null; @ Override public void onCreate (Bundle savedInstanceState) {super. o NCreate (savedInstanceState); this. mGameView = new GameView (this); // set to display as our custom View (GameView) setContentView (mGameView); // enable the Thread new Thread (new GameThread ()). start ();} Handler myHandler = new Handler () {// public void handleMessage (Message msg) {switch (msg. what) {case ControlView. REFRESH: mGameView. invalidate (); break;} super. handleMessage (msg) ;}}; class GameThread implements Runn Able {public void run () {while (! Thread. currentThread (). isInterrupted () {Message message = new Message (); message. what = ControlView. REFRESH; // send the message Activity01.this. myHandler. sendMessage (message); try {Thread. sleep (100);} catch (InterruptedException e) {Thread. currentThread (). interrupt () ;}}}// of course, these events can also be written in GameView. // The trigger event public boolean onTouchEvent (MotionEvent event) {return true ;} // press the key to event public boolean onKeyDown (int KeyCode, KeyEvent event) {return true;} // press the key to bring up the public boolean onKeyUp (int keyCode, KeyEvent event) {switch (keyCode) {// The case KeyEvent key on the right. KEYCODE_DPAD_UP: mGameView. y-= 3; break; // case KeyEvent. KEYCODE_DPAD_DOWN: mGameView. y + = 3; break;} return false;} public boolean onKeyMultiple (int keyCode, int repeatCount, KeyEvent) {return true ;}} package com. yarin. android. testOnView; im Port android. app. activity; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. view. keyEvent; import android. view. motionEvent; public class ControlView extends Activity {private static final int REFRESH = 0x000001; private GameView mGameView = null; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); this. mGameView = New GameView (this); // set to display as our custom View (GameView) setContentView (mGameView); // enable the Thread new Thread (new GameThread ()). start ();} Handler myHandler = new Handler () {// public void handleMessage (Message msg) {switch (msg. what) {case ControlView. REFRESH: mGameView. invalidate (); break;} super. handleMessage (msg) ;}}; class GameThread implements Runnable {public void run () {while (! Thread. currentThread (). isInterrupted () {Message message = new Message (); message. what = ControlView. REFRESH; // send the message Activity01.this. myHandler. sendMessage (message); try {Thread. sleep (100);} catch (InterruptedException e) {Thread. currentThread (). interrupt () ;}}}// of course, these events can also be written in GameView. // The trigger event public boolean onTouchEvent (MotionEvent event) {return true ;} // press the key to press the event public boolean onKeyDown (int keyCode, KeyEvent event) {return true;} // press the key to bring up the event public boolean onKeyUp (int keyCode, KeyEvent event) {switch (keyCode) {// case KeyEvent. KEYCODE_DPAD_UP: mGameView. y-= 3; break; // case KeyEvent. KEYCODE_DPAD_DOWN: mGameView. y + = 3; break;} return false;} public boolean onKeyMultiple (int keyCode, int repeatCount, KeyEvent event) {return true ;}}

 

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.