程序小白—-AndroidStudio之飞机大战
今天我们开始一些Android的学习
一:菜单类
新建一个菜单类用于定义一些变量和方法:
package com.example.xiao.fly;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.SurfaceHolder;
//以上为必要的包
/**
* Created by Xiao on 2017/5/24.
*/
public class GameMeun {
private Bitmap bmpMeunBG; //菜单背景图片
private Bitmap bmpLogo; //LOGO
private Bitmap bmpButton; //按钮
private Bitmap bmpText; //文本
private Rect rect; //创建一个矩形 用以方置LOGO
public GameMeun(Bitmap bmpMeunBG,Bitmap bmpLogo,Bitmap bmpButton,Bitmap bmpText){
this.bmpMeunBG=bmpMeunBG;
this.bmpLogo=bmpLogo;
this.bmpButton=bmpButton;
this.bmpText=bmpText;
rect = new Rect(0,GameSurface.hight/3,GameSurface.width,GameSurface.hight/3+GameSurface.hight/5);}
//这是关于矩形的大小位置设计
/*以下是关于该方法的API文档解释
* * @param left The X coordinate of the left side of the rectangle
* @param top The Y coordinate of the top of the rectangle
* @param right The X coordinate of the right side of the rectangle
* @param bottom The Y coordinate of the bottom of the rectangle
*/
public Rect(int left, int top, int right, int bottom) {
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
}
*/
/**
* 画背景图片
* @param canvas
* @param paint
*/
public void draw(Canvas canvas , Paint paint){
//画背景图片
canvas.drawBitmap(bmpMeunBG,0,0,paint);
//画logo
canvas.drawBitmap(bmpLogo,null,rect,paint);
//画按钮
int x = GameSurface.hight/3*2;
int y = GameSurface.width/2-bmpButton.getWidth()/2;
canvas.drawBitmap(bmpButton,y,x,paint); //前宽厚高
//画文本
int z = GameSurface.width/2-bmpText.getWidth()/2;
canvas.drawBitmap(bmpText,z,x,paint);
};
}
二:面板类:
package com.example.xiao.fly;
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 Xiao on 2017/5/24.
*/
public class GameSurface extends SurfaceView implements SurfaceHolder.Callback{
//定义一些必要的变量
private Canvas canvas;//画布
private Paint paint;//画笔
private SurfaceHolder surfaceHolder;
public static int width;
public static int hight;
//Meun相关的
//来自主菜单的一些变量
private GameMeun gameMenu;
private Bitmap bmpMeunBG;
private Bitmap bmpLogo;
private Bitmap bmpButton;
private Bitmap bmpText;
public GameSurface(Context context) {
super(context);
surfaceHolder = this.getHolder(); //初始化surfaceHolder
surfaceHolder.addCallback(this);//添加回调函数
paint = new Paint(); //初始化画笔
paint.setAntiAlias(true); //取消锯齿
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
width=this.getWidth(); //获取面板的宽度
hight=this.getHeight(); //获取面板的高度
intiBitmap();//初始化的方法
new Thread(new Runnable() {
@Override
public void run() {
Mydraw(); ///进入线程通过Mydraw开始写入到视图中
}
}).start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
/**
* 绘图方法
*/
private void Mydraw() {
canvas = surfaceHolder.lockCanvas(); //创建视图到画板上
gameMenu.draw(canvas,paint); //把所有的元素都布局到画板上
if (canvas!=null){
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
/**
* 初始化图片
*/
private void intiBitmap() {
//把图片转化为Bitmap格式
bmpMeunBG = 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 GameMeun(bmpMeunBG,bmpLogo,bmpButton,bmpText);
}
}