2012-04-13 68 views
0

我目前正在寫一的Android遊戲,但現在,我在與對照安卓遊戲循環surfaceview覆蓋與事業的FrameLayout滯後

一些奇怪的問題,如果我有我的所有控件的FrameLayout(惠普,點數,遊戲控制等),並將其放在我的表面視圖上,我的fps永遠不會超過20 fps;如果我刪除framelayout,遊戲將達到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的framelayout。

有沒有其他的選擇?可以爲UI繪製創建單獨的線程?或者我只是做錯了嗎?

這裏是我的XML狀態: enter image description here

+0

是'有史以來_run'設置爲'FALSE'? – slayton 2012-04-13 18:48:16

+0

是的,有一個函數調用改變_run當surfacedestroyed() – tom91136 2012-04-14 08:24:57

+0

我改變了問題,請再次檢查它.. – tom91136 2012-04-14 09:29:43

回答

0

解決,我不小心搞砸的FrameLayout,清潔工程是答案