程式小白----AndroidStudio之飛機大戰
阿新 • • 發佈:2019-02-20
程式小白—-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);
}
}