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:
- public class GameSurfaceView extends SurfaceView implements SurfaceHolder.Callback,Runnable {
- SurfaceHolder mSurfaceHolder = null;
- public GameSurfaceView(Context context) {
- super(context);
- mSurfaceHolder = this.getHolder();
- mSurfaceHolder.addCallback(this);
- this.setFocusable(true);
- }
- public void surfaceChanged(SurfaceHolder holder,int format,int width,int height){
- }
- public void surfaceCreated(SurfaceHolder holder ){
- new Thread(this).start();
- }
- public void surfaceDestroyed(SurfaceHolder holder){
- }
- public void run(){
- while (true){
- try{
- Thread.sleep(1000);
- }
- catch(Exception e){
- }
- synchronized(mSurfaceHolder){
- Draw();
- }
- }
- }
- private void Draw() {
- Canvas canvas= mSurfaceHolder.lockCanvas();
- ......
- mSurfaceHolder.unlockCanvasAndPost(canvas);
- }
- }
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.
- DisplayMetrics dm = new DisplayMetrics();
- getWindowManager().getDefaultDisplay().getMetrics(dm);
- int screenWidth = dm.widthPixels;
- 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.
- 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:
- Handler myHandler = new Handler (){
- Public void handleMessage (Message msg ){
- If (msg. what = 1 ){
- // Control the display of the page...
- }
- If (msg. what = 2 ){
- // Control the display of the page...
- }
- }
- };
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!