飞机大战的第一个页面GameSurface类
GamesurfaceView类方法
package com.example.chenning.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 ChenNing on 2017/5/24.
*/
public class GameSurfaceView extends SurfaceView implements SurfaceHolder.Callback{
//画布
private Canvas canvas;
//画笔
private Paint paint;
private SurfaceHolder surfaceHolder;
public static int screenWeight;
public static int screenheight;
private GameMenu gamemenu;
private Bitmap bmpMenuBG;//菜单页面背景图片
private Bitmap bmplogo;//菜单页面logo
private Bitmap bmgButton;//菜单页面Buffon
private Bitmap bmgText;//菜单页面文本
public GameSurfaceView(Context context) {
super(context);
surfaceHolder=this.getHolder();
surfaceHolder.addCallback(this);
paint = new Paint();
paint.setAntiAlias(true);//取消锯齿
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
screenWeight=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 initBitmap() {
//把图片转换成Bitmap类型
bmpMenuBG= BitmapFactory.decodeResource(this.getResources(),R.drawable.mainmenu);
bmplogo= BitmapFactory.decodeResource(this.getResources(),R.drawable.logo);
bmgButton= BitmapFactory.decodeResource(this.getResources(),R.drawable.menustart);
bmgText= BitmapFactory.decodeResource(this.getResources(),R.drawable.starttext);
//初始化对象
gamemenu=new GameMenu(bmpMenuBG,bmplogo,bmgButton,bmgText);
}
/**
* 绘图方法
*/
private void myDraw() {
//初始化canvas
canvas=surfaceHolder.lockCanvas();
//调用gameMenu对象游戏菜单页面
gamemenu.draw(canvas,paint);
if(canvas!=null){
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
GameMenu类
package com.example.chenning.myapplication;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
/**
* Created by ChenNing on 2017/5/24.
*/
public class GameMenu {
private Bitmap bmpMenuBG;//菜单页面背景图片
private Bitmap bmplogo;//菜单页面logo
private Bitmap bmgButton;//菜单页面Buffon
private Bitmap bmgText;//菜单页面文本
private Rect rect;
public GameMenu(Bitmap bmpMenuBG,Bitmap bmplogo,Bitmap bmgButton,Bitmap bmgText){
this.bmpMenuBG=bmpMenuBG;
this.bmplogo=bmplogo;
this.bmgButton=bmgButton;
this.bmgText=bmgText;
}
/**
* 画菜单页面
* @param canvas
* @param paint
*/
public void draw(Canvas canvas, Paint paint){
//画背景图
canvas.drawBitmap(bmpMenuBG,0,0,paint);
Rect rect=new Rect(0,GameSurfaceView.screenheight/3-100,
GameSurfaceView.screenWeight,
GameSurfaceView.screenheight/3+GameSurfaceView.screenheight/5-100);
canvas.drawBitmap(bmplogo,null,rect,paint);
int x=canvas.getWidth()/2-bmgButton.getWidth()/2;
int y=canvas.getHeight()/3*2;
canvas.drawBitmap(bmgButton,x,y,paint);
int z=canvas.getWidth()/2-bmgText.getWidth()/2;
int q=canvas.getHeight()/3*2+20;
canvas.drawBitmap(bmgText,z,q,paint);
}
}
mainActivity类
package com.example.chenning.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
//设置内容视图
setContentView(new GameSurfaceView(this));
}
}