關於飛機大戰的第一個頁面的製作
阿新 • • 發佈:2019-02-13
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);
}
}