wwqf的博客

笔记笔记

(笔记)炸弹人源码-之GameView基本结构设置背景图片(一)

/*先推gitHub-BomberMan源码*/

/*https://github.com/kuoruan/Android-BomberMan*/

/*这个项目不是我的,找项目时找到的*/


1. 在MainActivity添加:

requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
GameView gv = new GameView(this);
setContentView(gv);

疑点(1) -> 在源码中,为什么还要声明一个SurfaceHolder来转接?直接赋值不可以吗?


2.GameView基础结构以及设置背景

public class GameView extends SurfaceView implements SurfaceHolder.Callback{

    SurfaceHolder mGameSurfaceHolder;
    Context mGameContext;
    /*游戏主线程*/
    private GameThread mGameThread;
    /*主线程运行状况*/
    private boolean mIsGameRunning = true;
    /*背景图*/
    private Bitmap mBitmapBackgroundImage;
    private RectF mRectBackground;
    /*屏幕相关数据*/
    private int mScreenX = 0, mScreenY = 0;
    /*屏幕密度*/
    private float mScreenDensity = 0.0f;
    /*30毫秒刷新一次屏幕*/
    private final int TIME_IN_FRAME = 30;

    public GameView(Context context) {
        super(context);

        mGameContext = context;

        mGameSurfaceHolder = getHolder();
        mGameSurfaceHolder.addCallback(this);

        setZOrderOnTop(true);
        setFocusable(true);
        setFocusableInTouchMode(true);
        setKeepScreenOn(true);

        mGameThread = new GameThread();

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        mScreenX = w;
        mScreenY = h;
        mScreenDensity = Resources.getSystem().getDisplayMetrics().density;
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        if(mGameThread.getState() == Thread.State.TERMINATED) {
            mGameThread = new GameThread();
            mGameThread.setRunning(true);
            mGameThread.start();
        }else {
            mGameThread.setRunning(true);
            mGameThread.start();
        }
    }

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

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        mGameThread.setRunning(false);
    }


    private Bitmap loadImageByResId(int resId) {
        return BitmapFactory.decodeResource(getResources(), resId);
    }


    public class GameThread extends Thread {

        /*画布*/
        private Canvas canvas;

        public GameThread() {
            mBitmapBackgroundImage = loadImageByResId(R.drawable.game_bg);
        }

        @Override
        public void run() {

            long start = System.currentTimeMillis();

            try {
                synchronized (mGameSurfaceHolder) {
                    canvas = mGameSurfaceHolder.lockCanvas();
                    doDraw();
                }
            }catch (Exception e) {
                Log.e("GameView-run",e.getMessage());
            } finally{
                mGameSurfaceHolder.unlockCanvasAndPost(canvas);
            }

            long end = System.currentTimeMillis();

            /*确保每次更新屏幕的时间是30毫秒*/
            int diffentTime = (int)(end - start);
            while (diffentTime <= TIME_IN_FRAME) {
                diffentTime = (int)(System.currentTimeMillis() - start);
                /*Thread.yield() - 线程让步,由"运行状态"转换到"就绪状态"*/
                Thread.yield();
            }

        }

        /*绘制总入口*/
        private void doDraw() {
            if(canvas != null) {
                canvas.drawBitmap(mBitmapBackgroundImage, 0, 0, null);
            }
        }

        /*设置运行状态*/
        public void setRunning(boolean isRunning) {
            mIsGameRunning = isRunning;
        }
    }
}



疑点(2) -> canvas.drawBitmap(bitmap,float,float,paint);与canvas.drawBitmap(bitmap,matrix,RectF,paint);两者该怎么运用?


下一篇讲述在此基础上的场景设置.

阅读更多
文章标签: Android纯代码游戏
想对作者说点什么? 我来说一句

JAVA 炸弹人(一)

JAAV炸弹人

JackLei__ JackLei__

2016-01-09 22:27:35

阅读数:1112

没有更多推荐了,返回首页

不良信息举报

(笔记)炸弹人源码-之GameView基本结构设置背景图片(一)

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭