私は現在アンドロイドゲームを書いていますが、今、コントロールにいくつかの奇妙な問題があります
すべてのコントロール (HP、ポイント、ゲーム コントロールなど) を含むフレーム レイアウトがあり、それをサーフェス ビューの上に配置すると、fps が 20 fps を超えることはありません。フレームレイアウトを削除すると、ゲームは 50+fps になります。
システムが SurfaceView とフレームレイアウトのコンテンツを 1 つのスレッドでまとめてレンダリングしているのではないかと疑っています。これが非常に遅い理由を説明しています
ここに私のゲームループコードがあります
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状態があります: