Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我画线突然停止的原因?_Java_Android_Multithreading_Android Canvas - Fatal编程技术网

Java 我画线突然停止的原因?

Java 我画线突然停止的原因?,java,android,multithreading,android-canvas,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(); mContr

这就是我的线程基本上正在做的

@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.");
}
要限制每秒帧数,请在每次刷新之间留出某种睡眠时间。刷新方法如下所示:

公共同步的无效刷新(){ 整数旋转=数学圆((360+(-1)*mAccelerometer[0]*4.5f))%360); 移动对象设置旋转(旋转)

//从surfaceview获取画布
if(mSurfaceHolder!=null){
//做所有的计算。
if(Looper.myLooper()==Looper.getMainLooper()){
日志(“在主线程上!”);
}否则{
日志(“不在主线程上”);
}
Canvas mCanvas=mSurfaceHolder.lockCanvas();
如果(mCanvas!=null){
//移位x偏移
mScreenWidth=mCanvas.getWidth();
mScreenHeight=mCanvas.getHeight();
//绘制黑色背景,清除所有内容。
mCanvas.drawRect(新矩形(0,0,mScreenWidth,mScreenHeight),mBlackPaint);
//按住shift键并绘制所有视图对象(如果仍然可见)
对于(ViewObject视图:mViewObjectList.toArray(新的ViewObject[mViewObjectList.size()])){
view.shiftX(-1*选项、游戏性、环境、移动速度);
if(view.getX()+view.getWidth()<0){
mViewObjectList.remove(视图);
}
if(view.getCurrentBitmap()!=null)
mCanvas.drawBitmap(view.getCurrentBitmap()、新矩形(0,0,view.getCurrentBitmap().getWidth()、view.getCurrentBitmap().getHeight())、新矩形(view.getX()、view.getY()、view.getX()+view.getWidth()、view.getY()+view.getHeight()、new Paint());
view.nextFrame();
}
//绘图对象
最终位图sBitmap=mObject.getCurrentBitmap();
mObject.nextFrame();
如果(sBitmap!=null){
drawBitmap(sBitmap,new Rect(0,0,sBitmap.getWidth(),sBitmap.getHeight()),new Rect(mObject.getX(),mObject.getY(),sBitmap.getWidth(),sBitmap.getHeight()),new Paint());
}else日志(“位图=null!”);
}
mSurfaceHolder.解锁canvasandpost(mCanvas);
}
}
线程持续运行了一段时间,正在绘制背景元素(尽管我的旋转还没有完全工作,但这是另一个故事……),背景“似乎”在移动(像一个侧滚动条),但在某个随机时间点,没有任何ADB LogCat消息(不限于应用程序,而是整个LogCat输出)-线程只是停止。不再画画了。没有什么。我没有调用interrupt或将isRunning设置为false,因为“线程已死亡”。消息也没有写入LogCat。。 我不知道发生了什么事。 谢谢您的帮助。

您有:

    // 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);
    }
}
因此
System.currentTimeMillis()
将更晚,因此大于
timeDiff
。括号中的术语将是负数-因此您将添加到
timePerFrame
中,
timeDiff
将增长而不是减少

    // 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);
    }
}
timeDiff = System.currentTimeMillis();
// refresh, which takes some time I guess
timeDiff = timePerFrame - (timeDiff - System.currentTimeMillis());