【问题标题】:How to start Translate Animation in random order in Android?如何在Android中以随机顺序开始翻译动画?
【发布时间】:2015-02-04 05:25:43
【问题描述】:

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

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

【问题讨论】:

    标签: android animation random


    【解决方案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;
    
    }
    

    【讨论】:

    • 但它说的是随机顺序。我认为它像 3,2,1 或 2,1,3。只是我的想法
    • 你将如何随机选择 3 个可能的数字。 1 out of 3、2 out 3 或 3 out 3 的概率是多少?
    • 如果有帮助,请接受答案。问候
    【解决方案2】:

    这是我得到的一种方法,修改你的代码,@Murtaza Hussain

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

    【讨论】:

      猜你喜欢
      • 2018-01-01
      • 2011-05-27
      • 2013-10-16
      • 2012-04-13
      • 2011-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-16
      相关资源
      最近更新 更多