I understand that delay(), which is an alternative to Thread.sleep(), doesn’t block the UI thread. Does that mean I can create simple animations with it? The following code works, but is it okay to use it?
It’s OK to use as in it will not create blockages etc. The android infrastructure is however not expected to reliably schedule the continuation. Animation this way will be very stuttering.
A better idea of animation is often to take another approach. As long as you’re animating you invalidate the drawing as part of your onDraw. Then in your onDraw measure the time elapsed since the start of the animation. You position/adapt the elements according to this elapsed time so that even if a frame is dropped your animation is always at the right point, stutters less and automatically adapts to the framerate of the device.
It does animations in JavaFx using coroutines. However, instead of using delay it uses JavaFx.awaitPulse to suspend coroutine until the next animation tick. I don’t know if there is a similar concept in Android, but if there is, we can add the corresponding function to kotlinx-coroutines-android module, too.
Hmm. There appears to be a way to hook in to the infrastructure that is used by Animator subclases. There is a hidden AnimationHandler service used by ValueAnimators. A ValueAnimator subclass could be used to do so. To make it pretty you probably need some infrastructure to recalculate the correct values again (if you want delay dependent animation).