Andriod Studio 之飞机大战-构建游戏开始界面

1.主要思路:在Android Studio 中 新建游戏界面,并构造出主线程,另外在创建一个类作为游戏界面,在界面在新建一个方法,目的是交由游戏界面类调用;
2.具体方法(代码):

package com.example.fpl.helloworld;

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 FPL on 2017/5/24.
 */

public class GameSurface extends SurfaceView implements SurfaceHolder.Callback {
    private  GameMenu gameMenu;
    private Bitmap bmpMainBG;
    private Bitmap bmpLogo;
    private Bitmap bmpButton;
    private Bitmap bmpText;
    public  static  int screenwidth;
    public  static  int  screenheight;


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

    public GameSurface(Context context) {





        super(context);


        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();

    }

    private void mydraw() {
        canvas=surfaceHolder.lockCanvas();

       gameMenu.draw(canvas,paint);
        if (canvas!=null){
            surfaceHolder.unlockCanvasAndPost(canvas);

        }



    }
    private  void  initBitmap(){
        bmpMainBG= BitmapFactory.decodeResource(this.getResources(),R.drawable.mainmenu);
        bmpLogo= BitmapFactory.decodeResource(this.getResources(),R.drawable.logo);
        bmpButton= BitmapFactory.decodeResource(this.getResources(),R.drawable.menustartpress);
        bmpText= BitmapFactory.decodeResource(this.getResources(),R.drawable.starttext);



        gameMenu=new GameMenu(bmpMainBG,bmpLogo,bmpButton,bmpText);

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }
}










package com.example.fpl.helloworld;

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

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

public class GameMenu {
    private Bitmap bmpMainBG;
    private Bitmap bmpLogo;
    private Bitmap bmpButton;
    private Bitmap bmpText;
    private Rect rect;
    private Rect rect2;

public   GameMenu(Bitmap bmpMainBG,Bitmap bmpLogo,Bitmap bmpButton,Bitmap bmpText ){
    this.bmpButton=bmpButton;
    this.bmpLogo=bmpLogo;
    this.bmpMainBG=bmpMainBG;
    this.bmpText=bmpText;
    rect=new Rect(0,GameSurface.screenheight/6,GameSurface.screenwidth,GameSurface.screenheight/5+GameSurface.screenheight/5);
    rect2=new Rect(0,0,GameSurface.screenwidth,GameSurface.screenheight);

}

  public  void draw(Canvas canvas, Paint paint) {

      canvas.drawBitmap(bmpMainBG,null,rect2,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);
      int x1=GameSurface.screenwidth/2-bmpText.getWidth()/2;
      int y1=GameSurface.screenheight/3*2+20;
      canvas.drawBitmap(bmpText,x1,y1,paint);

    }

}

你可能感兴趣的:(Andriod Studio 之飞机大战-构建游戏开始界面)