简体   繁体   English

Android FPS,如何使其更流畅?

[英]Android FPS, how to make it smoother?

I am having trouble in constructing a basic game loop for my game. 我在为游戏构建基本游戏循环时遇到麻烦。 I am at the early phase and all I want to do is to move the the ball at a constant speed (for ex: 3) through the bottom of the screen. 我现在处于早期阶段,我要做的就是以恒定的速度(例如3)将球移过屏幕底部。 The position of the ball is updated in the main panel update method and it is drawed in the render part as expected. 球的位置在主面板更新方法中更新,并按预期方式在渲染部分中绘制。 Nothing exceptional in the mechanism. 该机制没有什么例外。 The ball moves but it is definitely not smooth, the visual is quiet disturbing.. I measured the implementation times of these methods and the total implementation time of this couple is about 3-4 milliseconds. 球移动了,但绝对不光滑,视觉上很安静。.我测量了这些方法的实现时间,这对夫妇的总实现时间约为3-4毫秒。 Under these circumstances, what is the suitable FPS? 在这种情况下,什么是合适的FPS? Are the given constant values suitable What is missing or wrong in my mechanism? 给定的常数是否合适我的机制中缺少或存在哪些错误? Thanks a lot in advance. 非常感谢。 the code block in main thread. 主线程中的代码块。

private final static int MAX_FPS = 200;
private final static int FRAME_PERIOD = 1000 / MAX_FPS;
private final static int    MAX_FRAME_SKIPS = 5;

public void run() {

    initTimingElements();
    long beginTime;
    long timeDiff;
    int sleepTime;
    int framesSkipped;
    long beginTime2;

    long diff1;
    long diff2;

    sleepTime = 0;

    Canvas canvas;
    Log.d(TAG, "Starting game loop");
    while (running) {
        canvas = null;
        try {
            canvas = this.surfaceHolder.lockCanvas();
            synchronized (surfaceHolder) {
                beginTime = System.currentTimeMillis();
                framesSkipped = 0; 

                this.gamePanel.update();
                diff1=System.currentTimeMillis()-beginTime;
                beginTime2=System.currentTimeMillis();
                this.gamePanel.render(canvas);
                diff2=System.currentTimeMillis()-beginTime2;



                timeDiff = System.currentTimeMillis() - beginTime;
                sleepTime = (int) (FRAME_PERIOD - timeDiff);

                if (sleepTime > 0) {
                    try {
                        Thread.sleep(sleepTime);
                    } catch (InterruptedException e) {
                    }
                }                   
                while (sleepTime < 0 && framesSkipped < MAX_FRAME_SKIPS) {
                    this.gamePanel.update();
                    sleepTime += FRAME_PERIOD;
                    framesSkipped++;
                }
                if (framesSkipped > 0) {
                    Log.d(TAG, "Skipped:" + framesSkipped);
                }
                framesSkippedPerStatCycle += framesSkipped;
                storeStats();
            }
        } finally {
            if (canvas != null) {
                surfaceHolder.unlockCanvasAndPost(canvas);
            }
        }
    }
}

Try using a TimerTask that fires off every 60ms or so. 尝试使用每60毫秒左右触发一次的TimerTask。
That's what I do until I stop being lazy enough to do a proper implementation, but it works well. 这就是我要做的,直到我不再懒得做适当的实现,但是效果很好。 Just remember that run() will be executed in a different thread, so use runOnUIThread() to run whatever code you need. 只需记住run()将在不同的线程中执行,因此请使用runOnUIThread()运行所需的任何代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM