forked from NativeScript/NativeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfps-native.android.ts
More file actions
48 lines (38 loc) · 1.34 KB
/
fps-native.android.ts
File metadata and controls
48 lines (38 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import definition = require("fps-meter/fps-native");
export class FPSCallback implements definition.FPSCallback {
private impl: android.view.Choreographer.FrameCallback;
private onFrame: (currentTimeMillis: number) => void;
public running: boolean;
constructor(onFrame: (currentTimeMillis: number) => void) {
this.running = false;
this.onFrame = onFrame;
this.impl = new android.view.Choreographer.FrameCallback({
doFrame: (nanos: number) => {
this.handleFrame(nanos);
}
});
}
public start() {
if (this.running) {
return;
}
android.view.Choreographer.getInstance().postFrameCallback(this.impl);
this.running = true;
}
public stop() {
if (!this.running) {
return;
}
android.view.Choreographer.getInstance().removeFrameCallback(this.impl);
this.running = false;
}
private handleFrame(nanos: number) {
if (!this.running) {
return;
}
// divide by 1 000 000 since the parameter is in nanoseconds
this.onFrame(nanos / 1000000);
// add the FrameCallback instance again since it is automatically removed from the Choreographer
android.view.Choreographer.getInstance().postFrameCallback(this.impl);
}
}