Basic development experience of Android games

Source: Internet
Author: User
Tags android games

Android game developmentThe basics and experience are the content to be introduced in this article, mainly to understand and learnAndroid game developmentContent instance, details aboutAndroid game developmentFor more information, see this article.

Create a file similar to the Russian squareAndroid game developmentThe game is completely touch-screen to implement some basic functions such as music playing. The game is based on android sdk1.6. As I first learned about android, this game was my first android game development, so there are still many shortcomings. I just want to share some of my experiences and tips during the development process, I hope this will be helpful to new users. Please do not give me any further advice.

I. First, we should know some basic frameworks for android game development.

In Android game development, the first is the View development framework. In Android game development, any View class only needs to override the onDraw method to display the interface. The core of the game is constant drawing and refreshing interfaces. The most common method in Android is to use Handler to update the UI thread. You can also directly use postInvalidate () in the run method () method to update the interface in the thread. Second, it is the SurfaceView class development framework.

When complex games need to be developed and the execution efficiency of programs is high, the View class cannot meet the requirements. This is the need to use the SurfaceView class for development, which is also the mainstream development framework. It must be created and destroyed during use, and monitored when the situation changes. This requires SurfaceHOlder. callback interface, if you want to cut the paintcloth to be drawn, you need to use SurfaceHOlder to control it for hours. In the program, the SurfaceHOlder object needs to be obtained through the getHolder method, and the addCallback method is also required to add a "callback function ".

For example, the following simple SurfaceView class framework:

 
 
  1. public class GameSurfaceView extends SurfaceView implements SurfaceHolder.Callback,Runnable {  
  2. SurfaceHolder mSurfaceHolder = null;  
  3. public GameSurfaceView(Context context) {  
  4. super(context);  
  5. mSurfaceHolder = this.getHolder();  
  6. mSurfaceHolder.addCallback(this);  
  7. this.setFocusable(true);  
  8. }  
  9. public void surfaceChanged(SurfaceHolder holder,int format,int width,int height){  
  10. }  
  11. public void surfaceCreated(SurfaceHolder holder ){  
  12. new Thread(this).start();  
  13. }  
  14. public void surfaceDestroyed(SurfaceHolder holder){  
  15. }  
  16. public void run(){  
  17. while (true){  
  18. try{  
  19. Thread.sleep(1000);  
  20. }  
  21. catch(Exception e){  
  22. }  
  23. synchronized(mSurfaceHolder){  
  24. Draw();  
  25. }  
  26. }  
  27. }  
  28. private void Draw() {  
  29. Canvas canvas= mSurfaceHolder.lockCanvas();  
  30. ......  
  31. mSurfaceHolder.unlockCanvasAndPost(canvas);  
  32. }  

In the activity class: setContentView (the object of GameSurfaceView ).

Ii. Basic Categories commonly used in Game Development

In Android game development, the graphics class is used to display 2D images. graphics includes Canvas, Paint, Color, and Bitmap.

For example, to draw a string, use the drawText method of the canvas class. drawRect is used to draw a rectangle. To set the transparency, use setAlpha of the paint class. At the same time, the Color. rgb method can directly obtain the Color represented by integer values. In addition, you must first load the image from the resource file before you can draw the image to the screen. The Code is as follows: Bitmap mypic = null; mypic = (BitmapDrawable) getResources (). getDrawable (R. drawable. pic )). getBitmap (); Canvas canvas = new Canvas (); canvas. drawBitmap (mypic, x, y, null );

Iii. Questions about adaptive screen resolution

Android game development in progressThe difference in the resolution of each mobile phone is destined to be a question that programmers must consider when developing a game. How can we write a program so that it can run well on machines with different resolutions, this will be one of the important factors that determine the quality of a game. There are three folders related to image storage under game resource res: drawable-hdpi, drawable-mdpi, and drawable-ldpi.

Drawable-hdpi stores high-resolution images, such as WVGA (480x800), FWVGA (480x854), and drawable-mdpi stores medium-resolution images, for example, HVGA (320x480), drawable-ldpi stores low-resolution images, such as QVGA (240x320 ), the system will automatically find the corresponding images in these folders Based on the machine resolution. Of course, in addition to preparing several sets of images with different resolutions, the coordinates of image coordinates and touch-screen events in the Code must be handled flexibly so that a program can run on multiple models.

 
 
  1. DisplayMetrics dm = new DisplayMetrics();  
  2. getWindowManager().getDefaultDisplay().getMetrics(dm);  
  3. int screenWidth = dm.widthPixels;  
  4. int screenHeight = dm.heightPixels; 

The above code can be used to obtain the screen pixels of the current model. However, over-column conversion can be used to flexibly implement coordinates in the code.

4. How to lock screen landscape or landscape

A game can be played without being designed as a landscape or landscape screen. It is fixed as a landscape or landscape screen.Android game developmentYou only need to configure it in AndroidManifest. xml and add this line.

 
 
  1. android:screenOrientation="landscape" 

For example, (landscape is landscape and portrait is portrait ).

V. navigation between views

In Android game development, a game does not have only one view, such as welcome animations, game images, menu pages, and so on. To achieve the jump between them, here is a method that is implemented using the Handler class. View the Code directly:

 
 
  1. Handler myHandler = new Handler (){
  2. Public void handleMessage (Message msg ){
  3. If (msg. what = 1 ){
  4. // Control the display of the page...
  5. }
  6. If (msg. what = 2 ){
  7. // Control the display of the page...
  8. }
  9. }
  10. };

Then, use myHandler. sendEmptyMessage (1) to implement the switchover.

Summary:Android game developmentThe basic information and experience are introduced.Android game developmentContent learning is helpful to you!

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.