如何计算SurfaceView刷新的帧率?

时间:2012-04-18 13:30:13

标签: android surfaceview

我有第三方框架,可在SurfaceView上显示视频。我想测量视频的帧率以检查手机是否能够足够快地显示视频,或者我是否需要使用其他解决方案向用户显示信息。

如何通过更新SurfaceView来测量速度?

2 个答案:

答案 0 :(得分:11)

如果代码不清楚,请询问。

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;
}

答案 1 :(得分:4)

试试这个!

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;
    }
}