如何在安卓中以随机顺序启动翻译动画



我有 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 个,不一定是第一个。

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

你可以在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;
}
这是我

得到的一种方式,修改你的代码,@Murtaza侯赛因

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

最新更新