按钮动画在一个线程上并在另一个线程中工作

时间:2013-09-14 05:43:26

标签: android multithreading runnable

我认为我的问题很基本,但我是android的新手,所以只需要这个解决方案。 我有20个按钮,所有这些按钮都在屏幕上显示动画(平移和缩放)。我想创建一个新线程并在那里做动画。这些按钮上有图像,代码在UI线程中工作正常但有时我得到ANR消息和异常,应用程序可能在其主线程上做了太多工作。 请帮忙 谢谢

这是我想要在线程内做动画的onCreate: -

    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);

}

1 个答案:

答案 0 :(得分:1)

你可以使用这样的东西:

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();