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。
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) {
}