Pregunta

I need to update my animations per frame, on iOS I have CADisplayLink, on WinPhone I have CompositionTarget, but how do I do that on Android?

Currently I'm using a Timer alongside a Handler, but I believe there is a way to sync the callback with the refresh rate.

¿Fue útil?

Solución 2

Hm, looks like ViewTreeObserver might help you there. Try this code:

final ViewTreeObserver vto = myView.getViewTreeObserver();
vto.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
    @Override
    public void onDraw() {
        // Do whatever you need to do...
    }
});

Couple of notes:

  • onDraw has bunch of limitations of what you can do in it. Specifically, you can't modify the view tree. If that's something you need to do, look at the ViewTreeObserver.OnPreDrawListener instead.
  • camera preview and media playback are drawn on a separate path, and most likely are not synchronized with this listener, so you wan't be able to use it to do stuff in sync with the individual frames.

Otros consejos

The most direct equivalent I'm aware of is Choreographer (as of API 16).

It looks like how your animations are written determines the best way to sync it. Choreographer is not generally useful. Choreographer is useful if your animation is written outside of the "animation framework" which AFAIK includes ViewPropertyAnimator and ... maybe XML defined animations? Exactly what is undocumented.

Choreographer looks like it's mainly useful for syncing GLSurfaceView animations.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top