如何在Android中以随机顺序开始翻译动画?

时间:2015-02-04 05:25:44

标签: android animation random

我有3个翻译动画在应用程序启动时播放 -

Animation animation = new TranslateAnimation(0, 0, -1500, 1500);
animation.setDuration(1000);
animation.setFillAfter(false);
myimage.startAnimation(animation);
animation.setRepeatCount(Animation.INFINITE);

Animation animation2 = new TranslateAnimation(0, 0, -1000, 1000);
animation2.setDuration(1000);
animation2.setFillAfter(false);
myimage2.startAnimation(animation2);
animation2.setRepeatCount(Animation.INFINITE);

Animation animation3 = new TranslateAnimation(0, 0, -500, 500);
animation3.setDuration(1000);
animation3.setFillAfter(false);
myimage3.startAnimation(animation3);
animation3.setRepeatCount(Animation.INFINITE); 

我想以随机顺序开始上述3,不一定是第一个。

一整天过去了,我仍然无法找到解决方案。 有关如何实现它的任何指示?

2 个答案:

答案 0 :(得分:1)

您可以在java中生成random数字并切换

Random random = new Random();
int num = 3;

switch(random.nextInt(num)) {
     case 0: 
         animateFirst();
          break;
     case 1: 
          animateSecond();
          break;
     case 2: 
         animateThird();
          break;

}

答案 1 :(得分:0)

这是我修改代码的一种方式,@ Murtaza Hussain

switch(random.nextInt(num)) {
         case 0: 
             animateFirst();
             animateSecond();
             animateThird();
              break;
         case 1: 
              animateSecond();
              animateFirst();
              animateThird();
              break;
         case 2: 
             animateFirst();
             animateThird();
             animateSecond();
              break;

    }