For a reaction time app, I'm trying to get the exact time when an image is displayed on the screen, after setting a new ImageDrawable
on an ImageView
.
Simply getting the time stamp after the image resource is set is not sufficiently accurate, because after ImageView.setImageDrawable()
Android still renders the image and waits for the next screen refresh before the image is actually displayed.
To get closer to the actual display time, I so far extended ImageView to schedule a Choreographer.FrameCallBack
in onDraw()
.
Choreographer.FrameCallback mFrameCallback = new Choreographer.FrameCallback() {
@Override
// From the documentation:
// This method provides the time in nanoseconds
// when the frame started being rendered.
public void doFrame(long frameTimeNanos) {
displayedAt = frameTimeNanos;
}
};
...
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Choreographer choreographer = Choreographer.getInstance();
choreographer.postFrameCallback(mFrameCallback);
}
However this method only gives the time system began rendering upcoming frame leaving the question of when the final callback is serviced and the pixels updated still undetermined - how can we determine the end of all doFrame()'s?