Android Runnable working only once

I am trying to rotate my image. I wrote my function to check rotate speed in the image. In the logic everything is ok but I am using runnable and the setrotate method is working only once.

This is a my source:

public void rotateimages() {
    myHandler = new Handler();
    runnable = new Runnable() {

        @Override
        public void run() {

            double k = 30;

            double speed = Math.PI / k;
            for (alpa = 0; alpa < (Math.PI * 6 + res * 2 * Math.PI / 5); alpa += speed) {
                latcirclelayout.setRotation(latcirclelayout.getRotation()
                        - ((float) alpa));

                k = Math.min(k + 0.2, 240);
                speed = Math.PI / k;
            }

        }
    };
    myHandler.postDelayed(runnable, 100);
}

I debugged it and the alpha value is changed everytime but the rotate is working only once. What am I doing wrong?

Jon Skeet
people
quotationmark

Your loop is just a tight loop - you're executing all the iterations in one go, basically, calling setRotation lots of times. I strongly suspect that all the calls to setRotation are being made... but you're not seeing anything but the last one. If you want to use this code for animation, you'd need to execute your code repeatedly - but only calling setRotation once per iteration.

You could check that by adding a log entry on each iteration of the loop - I'm sure you'll see that it executes all the iterations in very quick succession.

(I suspect there may be something else built into Android to make animation simpler than that, but I don't know offhand. The animation tutorial may well help you.)

people

See more on this question at Stackoverflow