Use of while in a thread on Android (runOnUiThread)

0

Hello, guys, I'm having the following problem I need to manage an animation. Which changes over time, and for this I made the following code:

public void HandleAnimator(){
    new Thread(new Runnable() {

        boolean pass1 = true;
        boolean pass2 = true;
        boolean pass3 = true;
        boolean pass4 = true;
        boolean pass5 = true;

        @Override
        public void run() {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {

                    SetAnimator(1); //Animação inicial

                    while(mCountDonwTime.getmTime()!=0) {
                        if(mCountDonwTime.getmTime()<80000 && pass2){
                            SetAnimator(2);
                            pass3 = false;
                        }
                        if(mCountDonwTime.getmTime() < 60000 && pass3){
                            SetAnimator(3);
                            pass4 = false;
                        }
                        if(mCountDonwTime.getmTime() < 40000 && pass4){
                            SetAnimator(4);
                            pass4 = false;
                        }
                        if(mCountDonwTime.getmTime() < 20000  && pass5){
                            SetAnimator(5);
                            pass5 = false;
                        }
                    }
                }
            });
        }
    }).start();
}

public void SetAnimator(int animacao){

    switch(animacao){
        case 1:
            im.setBackgroundResource(R.drawable.animation);
            break;
        case 2:
            im.setBackgroundResource(R.drawable.animationb);
            break;
        case 3:
            im.setBackgroundResource(R.drawable.animationc);
            break;
        case 4:
            im.setBackgroundResource(R.drawable.animationd);
            break;
        case 5:
            im.setBackgroundResource(R.drawable.animatione);
            break;
    }

    AnimationDrawable Animation = (AnimationDrawable) im.getBackground();
    Animation.start();
}

When I use this while within the structure of this thread, there is a type of overhead on the device's processor. So I do not know what else I could do to control these animations.

    
asked by anonymous 01.10.2015 / 00:45

1 answer

0

Only call the SetAnimator() on the UI thread using runOnUiThread() , and the loop itself on the thread you created. If I run the entire loop in the UI thread as it is doing it will give same problem.

    
01.10.2015 / 01:36