Android for loop not working in main theme

I am trying to rotate an image. i used runnable to rotate an image i wrote for a loop and i want to rotate the image inside the loop this is my source

public void rotateimages(final View myView) {
    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) {

                myView.setRotation((float) (myView.getRotation() - alpa));

                Log.e("alpa values", String.valueOf(alpa));

                k = Math.min(k + 0.2, 240);
                speed = Math.PI / k;
                myHandler.postDelayed(this, 100); 
            }
            // rotateimages();
        }

    };

    myHandler.postDelayed(runnable, 180);
}

      

When I launch my application, I can only rotate once. What's the mistake in my code? if anyone knows a solution please help me

+3


source to share


2 answers


If you want to run a loop, you need to define this "runnable" loop again, change your code to:

myHandler = new Handler();
count = 0;
public void rotateImage(final View myView, final int size) {


    runnable = new Runnable() {

        @Override
        public void run() {

            count++;
            myView.setRotation(myView.getRotation() + size);
            if(count == 10){
               myHandler.removeCallBack(runnable );
            }
           myHandler.postDelayed(this, 1000); // 1000 means 1 second duration
         }
        };
        myHandler.postDelayed(runnable, 180); // 180 is the delay after new runnable is going to called

}

      

How to loop yourself, it means that one line of code or one statement raises the event again. Example:



 void nothing(int a){
   if(a> 0)
     nothing(a-1);
   }

      

and not name anything (10). that's all, and avoid certain actions such as:

void nothing(int a){
       for(int i =0; i< a;i++) 
         nothing(a-1);                //BIG WRONG
       }
 Do you know your solution?

      

+1


source


In the for loop, you are constantly increasing the rotation of myView by 180.

If the number of iterations in the for loop is divisible by 2, then the resulting rotation will be the same as at the beginning. This is why if you rotate the shape once it becomes inverted and you can see a new rotation, but if you rotate it again it will come back again.

Illustration:

no flip. rotation: 0

#
#
# #

      

one flip. rotation: 180



# #
  #
  #

      

two coups. rotation: 360

#
#
# #

      

To fix your code the way you want it,

public void rotateImage(final View myView, final int size) {

    myHandler = new Handler();
    runnable = new Runnable() {

        @Override
        public void run() {

            // include one of the following:

            // Way #1: supports animation more easily
            for (int i = 0; i < size; i++) {
                myView.setRotation(myView.getRotation() + 1);
            }

            // Way #2: more performant way of doing it
            myView.setRotation(myView.getRotation() + size);

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

}

      

-1


source







All Articles