How to count the Framerate with which a SurfaceView refreshes?

前端 未结 2 633
再見小時候
再見小時候 2021-02-11 08:53

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 e

2条回答
  •  旧巷少年郎
    2021-02-11 09:12

    If the code is not clear just ask.

    LinkedList times = new LinkedList(){{
        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;
    }
    

提交回复
热议问题