飞机大战的第一个页面

飞机大战的第一个页面

GameSurface类

package com.example.asus.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 asus on 2017/5/24.
 */

public class GameSurface extends SurfaceView implements SurfaceHolder.Callback {

    private Canvas canvas;
    private Paint paint;
    private SurfaceHolder surfaceHolder;

    public static int screenWidth;
    public static int screenHeight;


    //menu相关的东西
    private  GameMenu gameMenu;
    private Bitmap bmpMenuBG;  //菜单页面背景图片
    private Bitmap bmpLogo;    //菜单页面logo
    private Bitmap bmpButton;   //菜单页面button
    private Bitmap bmpText;    //菜单页面文本


    public GameSurface(Context context) {
        super(context);
        surfaceHolder=this.getHolder();  //初始化holder
        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() {
                while (true) {
                    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();

        //调用GameSurface对象画游戏菜单页面
        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);

    }
    }

GameMenu类
package com.example.asus.myapplication;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;


/**
 * Created by asus on 2017/5/24.
 */

public class GameMenu {
    private Bitmap bmpMenuBG;  //菜单页面背景图片
    private Bitmap bmpLogo;    //菜单页面logo
    private Bitmap bmpButton;   //菜单页面button
    private Bitmap bmpText;    //菜单页面文本
    private Rect rect;    //矩形


    public GameMenu(Bitmap bmpMenuBG,Bitmap bmpLogo,Bitmap bmpButton,Bitmap bmpText){
        this.bmpMenuBG=bmpMenuBG;
        this.bmpLogo=bmpLogo;
        this.bmpButton=bmpButton;
        this.bmpText=bmpText;


        rect=new Rect(0,GameSurface.screenHeight/3,GameSurface.screenWidth,GameSurface.screenHeight/3+GameSurface.screenHeight/5);

    }
    /**
     * 画菜单页面
     */
    public void draw(Canvas canvas, Paint paint){
        //画背景图


        canvas.drawBitmap(bmpMenuBG,0,0,paint);
        canvas.drawBitmap(bmpLogo,null,rect,paint);


        int x=GameSurface.screenWidth/2-bmpButton.getWidth()/2;
        int y=GameSurface.screenHeight/3*2;

        canvas.drawBitmap(bmpButton,x,y,paint);
        canvas.drawBitmap(bmpText,x+bmpButton.getWidth()/6,y+bmpButton.getHeight()/6,paint);



    }
}

结果:

你可能感兴趣的:(代码)