package com.suzaku.ndk.anrtest;
import android.view.Choreographer;
/**
* @Author: kai.xu
* @CreateDate: 2022/5/19 3:49 下午
* @Description:
*/
public class FPSWatcher {
public interface Watcher {
void watch(int fps);
}
private long mLastFrameTimeNanos = 0;
private boolean isRunning = false;
private Watcher watcher;
private FPSWatcher() {
}
public void addWatcher(Watcher watcher) {
this.watcher = watcher;
}
public void start() {
if (isRunning) {
return;
}
mLastFrameTimeNanos = 0;
isRunning = true;
Choreographer.getInstance().postFrameCallback(frameCallback);
}
public void stop() {
mLastFrameTimeNanos = 0;
isRunning = false;
}
Choreographer.FrameCallback frameCallback = new Choreographer.FrameCallback() {
@Override
public void doFrame(long frameTimeNanos) {
//初始化时间
if (mLastFrameTimeNanos != 0) {
final long jitterNanos = frameTimeNanos - mLastFrameTimeNanos;
if (jitterNanos != 0) {
int fps = (int) ((1000000000.0 / jitterNanos) + 0.999);
if (watcher != null) {
watcher.watch(fps);
}
}
}
mLastFrameTimeNanos = frameTimeNanos;
//注册下一帧
if (isRunning) {
Choreographer.getInstance().postFrameCallback(this);
}
}
};
static class Inner {
private static final FPSWatcher watcher = new FPSWatcher();
}
public static FPSWatcher get() {
return Inner.watcher;
}
}
Android 监控FPS — Choreographer
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 1、需求 在用户操作手机时(或模拟用户操作手机),实时检测手机CPU、FPS、MEM、NET信息,确实此数据变化是...
- 2020-1-11 质量监控,资源使用 Memory, CUP, FPS https://www.jianshu....