本文介绍了如何计算与一个SurfaceView刷新帧率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个第三方的框架,显示了在 SurfaceView 。我想以测量视频的帧率,以检查是否在电话能够显示足够快的视频,或者如果我需要使用另一种解决方案,以显示信息给用户。

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.

我如何测量的速度,一个SurfaceView更新?

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

推荐答案

如果在code是不明确的只是问。

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

这篇关于如何计算与一个SurfaceView刷新帧率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 22:41