简体   繁体   English

在动画中使用5-6个线程是个好主意吗?

[英]Is it a good idea to use 5-6 threads in an animation?

I am using 5-6 threads for displayng one animation at a time.I am using translate animations as well as frame animations. 我一次使用5-6个线程显示一个动画,同时使用平移动画和帧动画。 Is this a good idea? 这是一个好主意吗? What is the best way to do it? 最好的方法是什么? My code is give below. 我的代码在下面给出。

Handler handler1 = new Handler();        
handler1.postDelayed(new Runnable() {
  public void run() {
    Animation animation = new TranslateAnimation(0 ,0 ,0 ,100 );
    animation.setDuration(800);
    ImageView rimg2 = (ImageView) findViewById(R.id.rainfall);
    animation.setRepeatCount(-1);
    rimg2.startAnimation(animation);

    Animation animation1 = new TranslateAnimation(-290, 290,0, -150);
    animation1.setDuration(35000);
    ImageView rimg = (ImageView) findViewById(R.id.cloud1);
    rimg.setVisibility(View.VISIBLE);
    animation1.setRepeatCount(-1);
    rimg.startAnimation(animation1);
  } 
},0);

Handler handler2 = new Handler();
handler2.postDelayed(new Runnable() {
  public void run() {
    Animation animation = new TranslateAnimation(0 ,0 ,0 ,100 );
    animation.setDuration(800);
    ImageView rimg3 = (ImageView) findViewById(R.id.drops);
    animation.setRepeatCount(-1);
    rimg3.startAnimation(animation);
    rimg3.setVisibility(View.VISIBLE);
  }
},1000);

Handler handler3 = new Handler();
handler3.postDelayed(new Runnable() {
  public void run() {
    Animation animation = new TranslateAnimation(0 ,0 ,-250 ,10 );
    animation.setDuration(800);
    ImageView rimg2 = (ImageView) findViewById(R.id.rainfall1);
    animation.setRepeatCount(-1);
    rimg2.startAnimation(animation);
    rimg2.setVisibility(View.VISIBLE);
    }
},5000);

Handler handler4 = new Handler();
handler4.postDelayed(new Runnable() {
  public void run() {
    Animation animation = new TranslateAnimation(0 ,0 ,-150 ,10 );
    animation.setDuration(800);
    ImageView rimg3 = (ImageView) findViewById(R.id.drops1);
    animation.setRepeatCount(-1);
    rimg3.startAnimation(animation);
    rimg3.setVisibility(View.VISIBLE);
    }
},10000);

Handler handler5 = new Handler();
handler5.postDelayed(new Runnable() {
  public void run() {
    Animation animation1 = new TranslateAnimation(-290, 290,0, -150);
    animation1.setDuration(35000);
    ImageView rimg = (ImageView) findViewById(R.id.cloud2);
    rimg.setVisibility(View.VISIBLE);
    animation1.setRepeatCount(-1);
    rimg.startAnimation(animation1);
  }
},15000);

Unless there is a reason not shown in your code sample, you do not need to post Runnables to run these animations. 除非代码示例中未显示原因,否则您无需发布Runnable即可运行这些动画。 You can start the animations from the main UI thread. 您可以从主UI线程开始动画。 For more details, check out the documentation here: 有关更多详细信息,请在此处查看文档:

http://developer.android.com/guide/topics/graphics/view-animation.html http://developer.android.com/guide/topics/graphics/view-animation.html

and here 和这里

http://developer.android.com/reference/android/view/animation/Animation.html http://developer.android.com/reference/android/view/animation/Animation.html

You can also set the StartOffset if you want to stagger the animation start times. 如果要错开动画的开始时间,也可以设置StartOffset。

And if you want to do something when the animation ends, you can use Animation.setAnimationListener 如果要在动画结束时执行某些操作,则可以使用Animation.setAnimationListener

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM