2

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

如何测量 SurfaceView 更新的速度?

4

2 回答 2

13

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

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;
}
于 2012-12-05T17:44:08.683 回答
5

试试这个!

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;
    }
}
于 2012-12-10T04:49:07.430 回答