1

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?

norlesh
  • 1,749
  • 11
  • 23
hgzech
  • 99
  • 1
  • 6
  • Try the Hugo library: https://github.com/JakeWharton/hugo – azurh Sep 06 '16 at 14:00
  • why don't you take the system.currenttimemillis() when image is displayed, and compare it with system.currenttimemillis() when user touches the screen – Antonios Tsimourtos Sep 06 '16 at 14:20
  • 1
    @Tony, this is exactly what I'm trying to do. I'm just wondering where exactly I should call system.currenttimemillis() to get the time the image is displayed on the screen. Is there a method which gets called exactly when the image shows up on the screen (i.e. "when the pixels light up")? – hgzech Sep 06 '16 at 14:27
  • can you share your code? when the user starts to "play"?, you could create the image view programmatically and add it to the view that you want, and that time you get the current time..when the user clicks on the image it calculates the difference and you can show it into a text for example – Antonios Tsimourtos Sep 06 '16 at 14:39
  • http://stackoverflow.com/questions/17606140/how-to-get-when-an-imageview-is-completely-loaded-in-android Look at the answer, inside the run you could get the current time – Antonios Tsimourtos Sep 06 '16 at 14:43
  • @Tony, thanks a lot for the answer! I am actually trying to get the exact time the image is displayed on the screen after a new drawable is set on an already existing ImageView (I edited my question). `ImageView.post()` gives me the time the ImageView is laid out, which already happens before the user starts to "play". – hgzech Sep 08 '16 at 08:48

0 Answers0