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状态:
是'有史以来_run'设置为'FALSE'? – slayton 2012-04-13 18:48:16
是的,有一个函数调用改变_run当surfacedestroyed() – tom91136 2012-04-14 08:24:57
我改变了问题,请再次检查它.. – tom91136 2012-04-14 09:29:43