飞机大战首页


package com.example.fly.myapplication;

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

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

public class GameMenu {
    private Bitmap bmpMenuBG;//菜单页面背景图片
    private Bitmap bmpMenuLogo;//菜单页面Logo
    private Bitmap bmpMenuButton;//菜单页面按钮
    private Bitmap bmpbtBG;//菜单页面按钮背景
    private Rect  rect;

    public GameMenu(Bitmap bmpMenuBG, Bitmap bmpMenuLogo, Bitmap bmpMenuButton, Bitmap bmpbtBG) {
        this.bmpMenuBG = bmpMenuBG;
        this.bmpMenuLogo = bmpMenuLogo;
        this.bmpMenuButton = bmpMenuButton;
        this.bmpbtBG = bmpbtBG;

        rect=new Rect
    (0,GameSurfaceview.screenHeight/3,GameSurfaceview.screenWidth,GameSurfaceview.screenHeight/3+GameSurfaceview.screenHeight/5);
    }//GameSurfaceview.screenHeight是调用这个高度;GameSurfaceview.screenWidth是调用这个宽度,四个变量,分别是左上角x,y坐标和右下角x,y坐标

    public void draw(Canvas canvas, Paint paint) {
        canvas.drawBitmap(bmpMenuBG, 0, 0, paint);

        canvas.drawBitmap(bmpMenuLogo, null, rect, paint);

        int x=GameSurfaceview.screenWidth/2-bmpMenuButton.getWidth()/2;
        int y=GameSurfaceview.screenHeight/3*2;
        canvas.drawBitmap(bmpMenuButton, x, y, paint);

        int m=GameSurfaceview.screenWidth/2-bmpbtBG.getWidth()/2;
        int n=GameSurfaceview.screenHeight/3*2;
        canvas.drawBitmap(bmpbtBG, m, n, paint);
    }
}

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

public class GameSurfaceview 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 bmpMenuLogo;//菜单页面Logo
    private Bitmap bmpMenuButton;//菜单页面按钮
    private Bitmap bmpbtBG;//菜单页面按钮背景

    public GameSurfaceview(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() {
                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对象画游戏菜单页面
        gameMenu.draw(canvas,paint);

        if(canvas!=null){
            surfaceHolder.unlockCanvasAndPost(canvas);
        }
    }

    /**
     * 初始化图片方法
     */
    private void initBitmap() {
        //把图片转化成Bitmap
        bmpMenuBG = BitmapFactory.decodeResource(this.getResources(), R.drawable.mainmenu);
        bmpMenuLogo = BitmapFactory.decodeResource(this.getResources(), R.drawable.logo);
        bmpMenuButton = BitmapFactory.decodeResource(this.getResources(), R.drawable.starttext);
        bmpbtBG = BitmapFactory.decodeResource(this.getResources(), R.drawable.menustart);
        //初始化对象
        gameMenu = new GameMenu(bmpMenuBG, bmpMenuLogo, bmpbtBG, bmpMenuButton);
    }
}



22:58:32

package com.example.fly.myapplication;

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

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

public class GameMenu {
    private Bitmap bmpMenuBG;//菜单页面背景图片
    private Bitmap bmpMenuLogo;//菜单页面Logo
    private Bitmap bmpMenuButton;//菜单页面按钮
    private Bitmap bmpbtBG;//菜单页面按钮背景
    private Rect  rect;

    public GameMenu(Bitmap bmpMenuBG, Bitmap bmpMenuLogo, Bitmap bmpMenuButton, Bitmap bmpbtBG) {
        this.bmpMenuBG = bmpMenuBG;
        this.bmpMenuLogo = bmpMenuLogo;
        this.bmpMenuButton = bmpMenuButton;
        this.bmpbtBG = bmpbtBG;

        rect=new Rect
    (0,GameSurfaceview.screenHeight/3,GameSurfaceview.screenWidth,GameSurfaceview.screenHeight/3+GameSurfaceview.screenHeight/5);
    }//GameSurfaceview.screenHeight是调用这个高度;GameSurfaceview.screenWidth是调用这个宽度,四个变量,分别是左上角x,y坐标和右下角x,y坐标

    public void draw(Canvas canvas, Paint paint) {
        canvas.drawBitmap(bmpMenuBG, 0, 0, paint);

        canvas.drawBitmap(bmpMenuLogo, null, rect, paint);

        int x=GameSurfaceview.screenWidth/2-bmpMenuButton.getWidth()/2;
        int y=GameSurfaceview.screenHeight/3*2;
        canvas.drawBitmap(bmpMenuButton, x, y, paint);

        int m=GameSurfaceview.screenWidth/2-bmpbtBG.getWidth()/2;
        int n=GameSurfaceview.screenHeight/3*2;
        canvas.drawBitmap(bmpbtBG, m, n, paint);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值