我的绘图线程突然停止的原因?

时间:2013-08-04 13:33:20

标签: java android multithreading android-canvas

@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消息(不限于App但整个LogCat输出) - 线程只是停止。没有更多的绘画。没有。我没有调用中断或将isRunning设置为false,因为“线程已死”。消息也没有写入LogCat .. 我不知道发生了什么。 谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

你有:

enter code here`timeDiff = System.currentTimeMillis();
// refresh, which takes some time I guess
timeDiff = timePerFrame - (timeDiff - System.currentTimeMillis());

因此System.currentTimeMillis()将会更晚,因此比timeDiff更大。括号中的术语将为负数 - 因此您将添加到timePerFrame上,timeDiff将不会减少。