您的位置:首页 > 其它

关于飞机大战的第一个页面的制作

2017-05-24 22:47 190 查看
 1.定义一个Gamesurface类继承SurfaceView再用implents关键字接口上SurfaceHolder.Callback然后再用Alt+回车导入未实现的方法(3个)声明Canvas(画布)paint(画笔)surfaceHolder。将GameMenu中声明的变量也在这里声明一下,实例化菜单,

2.在第一个方法Gamesurface  super.()下面实例化surfaceHolder=this.getHolder

再回调函数addcallbak传this进去

实例化paint=new Paint();再用setantialias消除锯齿传参为true

3.在surfacecreated方法中用一个线程调用Runable()方法,再在线程中写一个绘图的方法(不需要具体实现)讲绘图方法拿出来写到下面使代码更加整洁。在线程上面定义一个initBitmap()方法并定义一个screenwidth,screenheight用get方法调用得到屏幕高度宽度,初始化图片方法也将这个方法写在下面调用图片实例化声明的名称=BitmapFactory.decodeResouraces(this.getResouraces(),R.drawable.图片名称)这样依次调用

绘图方法在下面进行

先将canvas画布用surfaceHoder上锁

用gamemenu调用draw()方法传画笔尽进去,加if判断如果canvas为空则解锁。

4.新建一个类叫GameMenu的类用Bitmap声明bmpMenuBG(),bmplogo(),bmpbutton(),bmptext(),bmp()

4.新建一个(有参)构造方法GameMenu()讲声明的属性传参进去,用this.封装一下再新建一个画图方法传参画笔画布,再用canvas.drawBitmap调用

下面是具体代码:
GameSurface:

package com.example.administrator.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;

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

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

private GameMenu gamemenu;
private Bitmap bmpMenuBG;
private Bitmap bmplogo;
private Bitmap bmpButton;
private Bitmap bmpText;

public static int screenwidth;
public static int screenheight;

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) {
screenwidth=this.getWidth();
screenheight=this.getHeight();
initBitmap();
new Thread(new Runnable() {
@Override
public void run() {
Mydraw();
}

}).start();
}

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

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {

}
//绘图方法
private void Mydraw() {
//canvas进行锁定
canvas=surfaceHolder.lockCanvas();
gamemenu.draw(canvas,paint);
if (canvas!=null){
//解锁画布,呈现到视图接口
surfaceHolder.unlockCanvasAndPost(canvas);
}

}
//初始化图片方法
private void initBitmap() {
//把图片转换成Bitmap格式
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);
gamemenu=new GameMenu(bmpMenuBG,bmplogo,bmpButton,bmpText);
}

}
GameMenu:
package com.example.administrator.myapplication;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
;

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

public class GameMenu {
private Bitmap bmpMenuBG;
private Bitmap bmplogo;
private Bitmap bmpButton;
private Bitmap bmpText;
private Rect rect;

public GameMenu(Bitmap bmpMenuBG, Bitmap bmplogo, Bitmap bmpButton, Bitmap bmpText) {
this.bmpMenuBG = bmpMenuBG;
this.bmplogo = bmplogo;
this.bmpButton = bmpButton;
this.bmpText = bmpText;

rect=new Rect(0,GameSurface.screenheight/3,
GameSurface.screenwidth,
GameSurface.screenheight/3+GameSurface.screenheight/5);
}
public void draw(Canvas canvas, Paint paint){
canvas.drawBitmap(bmpMenuBG,0,0,paint);
canvas.drawBitmap(bmplogo,null,rect,paint);

int x=GameSurface.screenwidth/2-bmpButton.getWidth()/2;
int y=GameSurface.screenheight/3*2;
canvas.drawBitmap(bmpButton,x,y,paint);

int x1=GameSurface.screenwidth/2-bmpText.getWidth()/2;
int y1=GameSurface.screenheight/3*2;
canvas.drawBitmap(bmpText,x1,y1,paint);

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: