I want some ImageView to move to the right first, then down by pressing the button.
He wrote this:

private fun moveX() { ObjectAnimator.ofFloat(beatle, "translationX", getValue(curX)).apply { duration = 2000 start() } } private fun moveY() { ObjectAnimator.ofFloat(beatle, "translationY", getValue(curY)).apply { duration = 2000 start() } } private fun runClick(view: View) { while (curX + 1 < WIDTH) { curX++ moveX() } while (curY + 1 < HEIGHT) { curY++ moveY() } } 

still tried this:

 beatle.animate().translationX(getValue(x)).translationY(getValue(y)).setDuration(2000).start() 

In both cases, instead of going to the right and down in succession, my picture went diagonally, executing these commands simultaneously. Is it possible to write sequential animation using these classes? If not, what should I use?

    1 answer 1

    In your code, you give a request for an animation, but since it takes a long time, control often returns to the stream, so it turns out that 2 animations start right away. You need to wait for the end of one and only then start another.

    Try this:

     beatle .animate() .translationX(getValue(x)) .setDuration(2000) .withEndAction({ beatle .animate() .translationY(getValue(y)) .setDuration(2000) .start() }) .start() 
    • Thanks, your solution really solves the problem I described (for 2 consecutive commands). Is it possible to somehow make a sequential animation for an arbitrary number of commands? - Farit Avtakhov
    • Well also chained - Komdosh
    • either create a stream that listens for its turn, and set the flag in the withEndAction method - Komdosh
    • Thanks again. I probably misunderstood the word "stream", but my solution used a queue (dec) where I put all the necessary animations, which I then started recursively. It turned out some kind of offline solution. - Farit Avtakhov