Search code examples
androidmultithreadingrunnable

Button animations on one thread and working in another thread


I think my problem is very basic but i am new to android so just need this solution. I have 20 buttons and all of them are animating(translation and scaling) on the screen. I want to create a new thread and do animations there. Those buttons have images on them,code is working fine in UI thread but sometimes i am getting ANR message and exception that application may be doing too much work on its main thread. Please help Thank you

This is my onCreate in which i want to do animations inside thread:-

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    playerName=getSharedPreferences(WelcomeActivity.Player_Name,0);
    gameMode= getSharedPreferences("game_mode", 0);

    setContentView(R.layout.activity_dragonland);       
    shendron=(ImageView)findViewById(R.id.shendron);
    glaedr=(ImageView)findViewById(R.id.glaedr);
    saphira=(ImageView)findViewById(R.id.saphira);
    brownlay=(ImageView)findViewById(R.id.brownlay);
    chrysophylax=(ImageView)findViewById(R.id.chrysophylax);
    tempest=(ImageView)findViewById(R.id.tempest);
    thrisorn=(ImageView)findViewById(R.id.thrisorn);
    ruth=(ImageView)findViewById(R.id.ruth);
    grifoka=(ImageView)findViewById(R.id.grifoka);
    horrid=(ImageView)findViewById(R.id.horrid);
    brownmean=(ImageView)findViewById(R.id.brownmean);
    firnen=(ImageView)findViewById(R.id.firnen);
    rhaegal=(ImageView)findViewById(R.id.rhaegal);
    mnementh=(ImageView)findViewById(R.id.mnementh);
    gorep=(ImageView)findViewById(R.id.gorep);
    rubela=(ImageView)findViewById(R.id.rubela);
    hotrika=(ImageView)findViewById(R.id.hotrika);
    drako=(ImageView)findViewById(R.id.drako);
    cadui=(ImageView)findViewById(R.id.cadui);      
    balerion=(ImageView)findViewById(R.id.balerion);


    dragon_zoom = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.dragon_zoom);
    dragon_zoom.setStartOffset(1500);
    down_right = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.down_right); 
    up_right = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.up_right); 
    seq_down= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.sequential_down);
    seq_up= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.sequential_up);
    seq_right= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.sequntial_right);
    seq_left= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.sequential_left);


    shendron.startAnimation(dragon_zoom);
    glaedr.startAnimation(seq_down);
    saphira.startAnimation(down_right);
    brownlay.startAnimation(seq_up);
    chrysophylax.startAnimation(seq_right);
    tempest.startAnimation(up_right);
    thrisorn.startAnimation(dragon_zoom);
    ruth.startAnimation(down_right);
    grifoka.startAnimation(down_right);
    horrid.startAnimation(dragon_zoom);
    brownmean.startAnimation(dragon_zoom);
    firnen.startAnimation(dragon_zoom);
    rhaegal.startAnimation(seq_right);
    mnementh.startAnimation(up_right);
    gorep.startAnimation(seq_left);
    rubela.startAnimation(seq_left);
    hotrika.startAnimation(seq_left);
    drako.startAnimation(seq_left);
    cadui.startAnimation(dragon_zoom);
    balerion.startAnimation(up_right);

}

Solution

  • you can use something like this:

    Thread thread = new Thread() 
    { 
     @Override 
     public void run() { 
        try { 
                Animation ranim = (Animation) AnimationUtils.loadAnimation(getBaseContext(), 
                R.anim.rotation); 
                buttonRotate.setAnimation(ranim); 
        } catch (InterruptedException e) { 
            e.printStackTrace(); 
        } 
     } 
    }; 
    
    thread.start();