【问题标题】:How to count the Framerate with which a SurfaceView refreshes?如何计算 SurfaceView 刷新的帧率?
【发布时间】:2012-04-18 13:30:12
【问题描述】:

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

如何测量 SurfaceView 更新的速度?

【问题讨论】:

标签: android surfaceview


【解决方案1】:

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

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】:

试试这个!

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-16
    • 1970-01-01
    • 1970-01-01
    • 2013-02-26
    • 1970-01-01
    • 1970-01-01
    • 2019-12-27
    • 1970-01-01
    相关资源
    最近更新 更多