Android 项目之飞机大战

首先,我们要创建一个GameSurface()类;我们此次采用的是画登入界面的方式,所以GameSurface()需要继承SurfaceView类而且要执行SurfaceHolder.Callback的方法,并且实现其中的没有完成的方法surfaceChanged、surfaceDestroyed、surfaceCreated、GameSurface。

 第二步我们需要创建SurfaceHolder 的对象surfaceHolder,这个是包装类;再创建画布类Canvas的对象canvas和画笔类Paint对象paint并用private修饰。

 第三:在GameSurface类中初始化surfaceHolder;再添加回调函数surfaceHolder = this.getHolder();接着我们初始化画笔;再添加抗锯齿paint.setAntiAlias();其中的参数为true。

 第四:在surfaceCreated方法中添加一个新的线程,在run()方法中写一个myDraw()方法并用快捷键实现。再在myDraw()方法中先将画布加锁surfaceHolder.lockCanvas();再判断画布是否为空如果不是则将画布显示在模拟器上;这是模拟器呈现的是一片黑色的背景。

 第五:这时我们需要把制作飞机大战的图片拷贝至res中的deawable中;再在GameSurface()中创建bmpMenuBG,Logo:bmplogo,按钮:bmpButton,文本:bmptext。再用private修饰,基于Bitmap类创建;接着我们在GameSurface()中创建一个方法initBitmap()导入我们需要的图片用bmpMenuBG=BitmapFactory.decodeResource(this.getResources(), R.drawable.mainmenu);

其中最后的mainmenu就是我们添加的图片。 然后我们就可以开始画了。 
我们先创建一个GameMenu()类,将我们需要的页面背景图片对象:bmpMenuBG,Logo:bmplogo,按钮:bmpButton,文本:bmptext。再用private修饰,基于Bitmap类创建 
在创建构造方法GameMenu(),封装我们创建的四个对象。 
在创建一个普通的方法myDraw(),其中传入画布Canvas的对象canvas和画笔Paint的对象paint就可以开始”画”了。 
第一步先将画布背景换成我们需要的背景图案 
用canvas调用drawBitmap方法其中需要四个参数第一个是我们第一的背景图片bmpMenuBG,第二个数字和第三个数字是图片的位置,由于是背景图片我们就填两个0,第四个是画笔对象paint。 
接着我们要把飞机大战游戏Logo画上去,但是现在我们不是背景图了,所以我们要创建一个Rect()矩形方法对象r,将Logo填在这个矩形中,再将矩形放在画布上的准确位置。初始化Rect()中也要填写四个参数分别是矩形左边距离屏幕左边的距离、上边距离屏幕上边的距离、右边距离屏幕右边的距离、下边距离屏幕下边的距离。这时这四个参数需要我们好好斟酌。 
ps:此次我们用的是小米5splus手机调试 
我们用int定义一个width=canvas.getWidth()来获取模拟器宽,height=canvas.getHeight()获取模拟器高;经过调试在小米5Splus中需要填写 
(50,(int)(height*0.05),width-50,(int)(height*0.3+height*0.15)比较合适(根据手机不通参数不同),再用canvas调用drawBitmap 
在第一个改为bmplogo对象,第二个参数时填写null,第三个改为r,第四个依旧是paint。 
接着我们画按钮先定义一个gao,kuan;再经过调试gao= (int) (height*0.75);和kuan=width/2-bmpButton.getWidth()/2;;最后用canvas调用drawBitmap在第一个改对象bmpButton,第二个参数时填写kuan,第三个改为gao,第四个依旧是paint。最后我们在按钮上再加一个印有汉字:开始游戏1的图片再次定义gao和kuan,调试后为gao= (int) (height*0.75)+20;kuan=width/2-bmpButton.getWidth()/2+40;最后用canvas调用drawBitmap 
在第一个改为bmptext对象,第二个参数时填写gao,第三个改为kuan,第四个依旧是paint。 
至此我们完成了所有代码: 
`package com.lenovo.myapplication;

import Android.graphics.Bitmap; 
import android.graphics.Canvas; 
import android.graphics.Paint; 
import android.graphics.Rect; 
import android.view.Surface; 
import android.view.SurfaceHolder;

/** 
* Created by HuangYong on 2017/5/24. 
*/

public class GameMenu {

private Bitmap bmpMenuBG;//菜单页面背景图片

private Bitmap bmplogo;//菜单页面LOGO

private Bitmap bmpButton;//菜单页面button

private Bitmap bmptext;//菜单页面文本

 

public GameMenu(Bitmap bmpMenuBG, Bitmap bmplogo, Bitmap bmpButton, Bitmap bmptext) {

    this.bmpMenuBG = bmpMenuBG;

    this.bmplogo = bmplogo;

    this.bmpButton = bmpButton;

    this.bmptext = bmptext;

}

 

public  void  myDraw(Canvas canvas, Paint paint){

    //菜单背景

    canvas.drawBitmap(bmpMenuBG,0,0,paint);

    //画logo

    int width = canvas.getWidth();//获取屏幕宽

    int height=canvas.getHeight();//获取屏幕高

    Rect r = new Rect(50,(int)(height*0.05),width-50,(int)(height*0.3+height*0.15));

    int gao= (int) (height*0.10);

    int kuan=width/2-bmplogo.getWidth()/2;

    canvas.drawBitmap(bmplogo,null,r,paint);

    //按钮

    gao= (int) (height*0.75);

    kuan=width/2-bmpButton.getWidth()/2;

    canvas.drawBitmap(bmpButton,kuan,gao,paint);

    //文字

    gao= (int) (height*0.75)+20;

    kuan=width/2-bmpButton.getWidth()/2+40;

    canvas.drawBitmap(bmptext,kuan,gao,paint);

}



package com.lenovo.myapplication;

import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Paint; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView;

import com.lenovo.myapplication.R;

/** 
* Created by HuangYong on 2017/5/24. 
*/

public class GameSurface extends SurfaceView implements SurfaceHolder.Callback { 
private SurfaceHolder surfaceHolder;//包装类 
private Canvas canvas;//画布 
private Paint paint;//画笔

private Bitmap bmpMenuBG;//菜单页面背景图片

private Bitmap bmplogo;//菜单页面LOGO

private Bitmap bmpButton;//菜单页面button

private Bitmap bmptext;//菜单页面文本

 

public GameSurface(Context context) {

    super(context);

// 初始化surfaceHolder 
surfaceHolder = this.getHolder(); 
// 添加回调函数 
surfaceHolder.addCallback(this); 
// 初始化画笔 
paint = new Paint(); 
paint.setAntiAlias(true); //抗锯齿 
}

@Override

public void surfaceCreated(SurfaceHolder holder) {

    initBitmap();

    new Thread(new Runnable() {

        @Override

        public void run() {

            myDraw();

        }

 

 

    }).start();

 

}

 

 

private void initBitmap() {

    //        图片导入

    bmpMenuBG = BitmapFactory.decodeResource(this.getResources(), R.drawable.mainmenu);

    bmplogo = BitmapFactory.decodeResource(this.getResources(), R.drawable.logo);

    bmpButton = BitmapFactory.decodeResource(this.getResources(), R.drawable.menustart);

    bmptext = BitmapFactory.decodeResource(this.getResources(), R.drawable.starttext);

}

 

private void myDraw() {

// 画布加锁初始化 
canvas = surfaceHolder.lockCanvas(); 
// 初始化游戏启动界面 
new GameMenu(bmpMenuBG, bmplogo, bmpButton, bmptext).myDraw(canvas, paint); 
// 释放画布 
if (canvas != null) { 
surfaceHolder.unlockCanvasAndPost(canvas); 

}

@Override

public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

 

}

 

@Override

public void surfaceDestroyed(SurfaceHolder holder) {

 

}

}

猜你喜欢

转载自blog.csdn.net/ll592316/article/details/72721889