@Override
public void run() {
super.run();
final float timePerFrame = 1000/Options.MAX_FPS;
float timeDiff;
while(!isInterrupted() && isRunning) {
timeDiff = System.currentTimeMillis();
mController.refresh();
timeDiff = timePerFrame - (timeDiff - System.currentTimeMillis());
try {
Thread.sleep(Math.max(Math.round(timeDiff), 0));
} catch(InterruptedException e) {
// do nothing
e.printStackTrace();
}
}
Log.e("GameThread", "Thread dead.");
}
이것은 내 스레드가 기본적으로 수행하는 것입니다.내 드로잉 스레드가 갑자기 멈추는 이유는 무엇입니까?
Options.MAX_FPS = 30;
초당 프레임 수를 제한하여 각 새로 고침 사이에 일종의 수면 시간을 갖습니다. 새로 고침 방법은 다음과 같습니다.
public synchronized void refresh() { int mRotation = Math.round (360 + ((-1) * mAccelerometer [0] * 4.5f)) % 360); mObject.setRotation (mRotation);
// get canvas from surfaceview
if(mSurfaceHolder != null) {
// do all calculations.
if(Looper.myLooper() == Looper.getMainLooper()) {
log("on main thread!");
} else {
log("Not on main thread");
}
Canvas mCanvas = mSurfaceHolder.lockCanvas();
if (mCanvas != null) {
// shift x offset
mScreenWidth = mCanvas.getWidth();
mScreenHeight = mCanvas.getHeight();
// draw black background, clear everything.
mCanvas.drawRect(new Rect(0,0,mScreenWidth, mScreenHeight), mBlackPaint);
// shift & draw all viewobjects if still visible
for(ViewObject view: mViewObjectList.toArray(new ViewObject[mViewObjectList.size()])) {
view.shiftX(-1 * Options.Gameplay.Environment.MOVING_SPEED);
if(view.getX() + view.getWidth() < 0) {
mViewObjectList.remove(view);
}
if(view.getCurrentBitmap() != null)
mCanvas.drawBitmap(view.getCurrentBitmap(), new Rect(0,0,view.getCurrentBitmap().getWidth(), view.getCurrentBitmap().getHeight()), new Rect(view.getX(), view.getY(), view.getX()+ view.getWidth(), view.getY()+view.getHeight()), new Paint());
view.nextFrame();
}
// draw object
final Bitmap sBitmap = mObject.getCurrentBitmap();
mObject.nextFrame();
if(sBitmap != null) {
mCanvas.drawBitmap(sBitmap, new Rect(0, 0, sBitmap.getWidth(), sBitmap.getHeight()), new Rect(mObject.getX(), mObject.getY(), sBitmap.getWidth(), sBitmap.getHeight()), new Paint());
} else log("bitmap = null!");
}
mSurfaceHolder.unlockCanvasAndPost(mCanvas);
}
}
스레드가 얼마 동안 계속 실행, 배경 요소가 그려되고있다 (내 회전이 아직은 작동하지 않습니다, 그러나 그것은 또 다른 이야기 ... 비록), 배경은 측면처럼 (이동 "보인다" (ADB LogCat) 메시지 (애플리케이션에만 제한되지 않고 전체 LogCat 출력)없이 스레드가 멈 춥니 다. 더 이상 그림이 없습니다. 아무것도. 저는 인터럽트를 호출하거나 isRunning을 false로 설정하지 않습니다. "스레드가 죽었습니다." 메시지는 LogCat에도 쓰여지지 않습니다 .. 무슨 일이 일어나고 있는지 모르겠습니다. 도움 주셔서 감사합니다.