0

我目前正在编写一个 android 游戏,但现在,我遇到了一些奇怪的控件问题

如果我有一个包含所有控件(HP、点、游戏控件..等)的框架布局并将其放在我的表面视图之上,我的 fps 将永远不会超过 20 fps;如果我删除框架布局,游戏将达到 50+fps。

我怀疑系统将我的surfaceview和framelayout的内容全部渲染在一个线程中,这解释了为什么它这么慢

这是我的游戏循环代码

public void run() {
    Canvas c;
    initTimingElements();

    long beginTime;     // the time when the cycle begun
    long timeDiff;      // the time it took for the cycle to execute
    int sleepTime;      // ms to sleep (<0 if we're behind)
    int framesSkipped;  // number of frames being skipped 

    sleepTime = 0;

    while (_run) {
        c = null;
        //long start = System.currentTimeMillis(); 

        try {

            c = _panel.getHolder().lockCanvas(null); // this is the problem!!

            synchronized(_panel.getHolder()) {


                beginTime = System.currentTimeMillis();
                framesSkipped = 0;  // resetting the frames skipped

                    _panel.updatePhysics();                     
                    _panel.checkForHits();
                    _panel.checkForKill();
                    _panel.checkForMCHits();
                    //if i put stuff related to ui in the loop, it will slow the control down to a unblelieveable state 
                    //_panel.checkNPCQt();
                    _panel.onDraw(c);


                    timeDiff = System.currentTimeMillis() - beginTime;
                    // calculate sleep time
                    sleepTime = (int)(FRAME_PERIOD - timeDiff);

                    if (sleepTime > 0) {
                        // if sleepTime > 0 we're OK
                        /*
                        try {
                            // send the thread to sleep for a short period
                            // very useful for battery saving
                            Thread.sleep(sleepTime);
                        } catch (InterruptedException e) {}
                        */

                    }

                    while (sleepTime < 0 && framesSkipped < MAX_FRAME_SKIPS) {
                        // we need to catch up
                        //_panel.updatePhysics(); // update without rendering
                        sleepTime += FRAME_PERIOD;  // add frame period to check if in next frame
                        framesSkipped++;
                    }

                    if (framesSkipped > 0) {
                        Log.d(TAG, "Skipped:" + framesSkipped);
                    }
                    // for statistics
                    framesSkippedPerStatCycle += framesSkipped;
                    // calling the routine to store the gathered statistics
                    storeStats();
            }

        } finally {
            // do this in a finally so that if an exception is thrown
            // during the above, we don't leave the Surface in an
            // inconsistent state
            if (c != null) {
                _panel.getHolder().unlockCanvasAndPost(c);
                //Log.d(TAG, "unlock canvas");
            }
        }

我很确定我所有的游戏逻辑和位图都是正确的;如果我注释掉所有 onDraw 函数和其他所有内容,我仍然只有 20-23 fps 的帧布局。

有其他选择吗?可以为 UI 绘制创建一个单独的线程吗?还是我做错了?

这是我的 XML 状态: 在此处输入图像描述

4

1 回答 1

0

解决了,我不小心弄乱了框架布局,干净的项目就是答案

于 2012-04-14T14:36:31.737 回答