【问题标题】:Limit Frame per second每秒限制帧数
【发布时间】:2013-01-31 22:06:28
【问题描述】:

大家好,我正在尝试使用以下代码将我的 FPS 限制为 30,但我不确定它是否有效。

while (runthread){

            c = null;
            timestart = System.currentTimeMillis();                         //Get time at start of loop for FPS calc

            try{
            c=mySurfaceHolder.lockCanvas();                                 //Set c (Canvas) to locked

            synchronized(mySurfaceHolder){
            framesskipped = 0;                                              // resetting frames skipped (as this is about to render to the screen, the frames skipped variable is only incremented when the onDraw() method is skipped, once it's run, reset it back to 0 for the next count
            doDraw(c);                                                      //Draw game world
            updateMenu();  //Update game logic

            }
            }
            finally{
                if (c != null){

                mySurfaceHolder.unlockCanvasAndPost(c);                     //Unlock and post

                }
            }

            //work out timings

                timeend = System.currentTimeMillis();                       //get end time for current frame (for FPS) 
                frametime = timeend-timestart;                              //Set the frametime variable to the time the frame took to render & update (end time - start time)
                sleepfor = (int) (30.3030303030303-frametime);              // this is the time that the thread will sleep for if <target time (1000/33 = apx. 30FPS


                if (sleepfor>0){                                            // If the 'sleepfor' variable is >0 then set the thread to sleep for it's value (expressed in milliseconds)

                    try {
                        MainThread.sleep(sleepfor);                         //send thread to sleep for value of sleepfor (determined above).

                    } catch (InterruptedException e) {}                     //in case of exception
                }                                                           //close if statement


                    while (sleepfor<0 && framesskipped<maxframesskipped){   //if sleepfor is < 0 (ie, frame took longer to render than target time and the maxframesskipped has not reached it's limit)
                        updateMenu();                                       //Update game logic only
                        sleepfor+=33;                                       //time to sleep plus the time frame took to render
                        framesskipped++;                                    //add one to framesskipped variable so this only skips a certain number of frames
                        frame=0;
                        }

我在 onDraw 方法中的日志输出。

如您所见,它看起来并没有限制在 30fps,我不知道为什么,我的数字一定是错误的。

其次,任何人都知道为什么我的帧数不时下降到 5 吗?!

谢谢

【问题讨论】:

  • 太棒了!完全没有理由投下反对票的反对票 - 我对这些感到非常兴奋,他们很有帮助 - 谢谢!!

标签: android screen frame-rate timedelta


【解决方案1】:

我发现了问题所在:

sleepfor = (int) (30.3030303030303-frametime);

应该是

sleepfor = (int) ((1000/fps)-frametime);

所以 sleepfor = (int) (33.3333333-frametime);

改变了它,它在 30 / 31 fps 下运行得很好

【讨论】:

    猜你喜欢
    • 2021-03-22
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多