How to count the Framerate with which a SurfaceView refreshes?

喜欢而已 提交于 2019-12-09 07:13:24

问题


I have a third party framework that shows video on a SurfaceView. I would like to measure the framerate of the video to check if the phone is capable of showing the video fast enough, or if I need to use another solution to show the information to the user.

How can I measure the speed with that a SurfaceView is updated?


回答1:


If the code is not clear just ask.

LinkedList<Long> times = new LinkedList<Long>(){{
    add(System.nanoTime());
}};

@Override
protected void onDraw(Canvas canvas) {
    double fps = fps();
    // ...
    super.onDraw(canvas);
}

private final int MAX_SIZE = 100;
private final double NANOS = 1000000000.0;

/** Calculates and returns frames per second */
private double fps() {
    long lastTime = System.nanoTime();
    double difference = (lastTime - times.getFirst()) / NANOS;
    times.addLast(lastTime);
    int size = times.size();
    if (size > MAX_SIZE) {
        times.removeFirst();
    }
    return difference > 0 ? times.size() / difference : 0.0;
}



回答2:


try this!

private long mLastTime = 0;
private int fps = 0, ifps = 0;
@Override
protected void onDraw(Canvas canvas) {
    long now = System.currentTimeMillis();

    // perform other operations

    System.out.println("FPS:" + fps);

    ifps++;
    if(now > (mLastTime + 1000)) {
        mLastTime = now;
        fps = ifps;
    ifps = 0;
    }
}


来源:https://stackoverflow.com/questions/10210439/how-to-count-the-framerate-with-which-a-surfaceview-refreshes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!