Use of while in a Thread on Android(runOnUiThread)

Asked

Viewed 149 times

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 kind of overload in the device processor. So I don’t know what else I could do to control these animations.

1 answer

0


Just run the call to SetAnimator() in the UI thread using runOnUiThread(), and the loop itself in the thread you created. Running the entire loop in the UI thread as you are doing will give problem even.

  • I managed to solve here with an alternative solution but thanks =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.