飞机大战1

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //  setContentView(R.layout.activity_main);
        //  setContentView(new MySurfaceview(this));
        setContentView(new GameSurface(this));


    }

}

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.example.y.myapplication.R;

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

public class GameSurface extends SurfaceView implements SurfaceHolder.Callback{
    private SurfaceHolder surfaceHolder;
    private Canvas canvas;
    private Paint paint;

    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();
        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();
        // 调用GameMenu对象画游戏菜单页面
        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);
    }
}
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

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


public class GameMenu {
    private Bitmap bmpMenuBG;
    private Bitmap bmpLogo;
    private Bitmap bmpButton;
    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);
    }

    /**
     * 画菜单页面
     * @param canvas
     * @param paint
     */
    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);
        int z=GameSurface.screenWidth/2-bmpButton.getWidth()/3;
        int s=GameSurface.screenHeight/3*2+20;
        canvas.drawBitmap(bmpText,z,s,paint);


    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值