2015-04-22 5 views
0

Я пишу приложение «Space Invaders», и мне нужно было сделать повторную анимацию перемещения, которая является классической для игры. Я получил процессы, чтобы идти в одну сторону, используя код ниже, однако, когда я хочу вернуться назад, похоже, нет простого способа сделать это. Я думаю, что самый простой способ сделать это - это что-то случилось в конце анимационного события, однако я не могу заставить событие запускать, даже при вызове super.end();. Я искал способ запуска события вручную, но безрезультатно. То, как я это делаю, может быть немного отрывочным, но сейчас он работает достаточно хорошо, чтобы попытаться заставить его работать до конца.Custom Animation end trigger

public void start() 
{ 
    if(begin < end) //recursive end condition 
    { 
     int distance = interval + begin; //change the distance to be traveled 
     //create new animation to do part of the whole animation 
     ObjectAnimator anim = ObjectAnimator.ofFloat(toAnimate, property, begin,distance); 
     TimeInterpolator inter = new TimeInterpolator() //makes the animation move with only one frame 
     { 
      public float getInterpolation(float prog) 
      { 
       return Math.round(prog * 10)/10; 
      } 
     }; 
     anim.setInterpolator(inter); 
     anim.setDuration((long)(500)); 
     anim.addListener(new AnimatorListener() 
     { 
      @Override 
      public void onAnimationStart(Animator animation){} 
      @Override 
      public void onAnimationEnd(Animator animation) 
      { 
       start(); //start the next part of the movement 
      } 
      @Override 
      public void onAnimationCancel(Animator animation){} 
      @Override 
      public void onAnimationRepeat(Animator animation){} 
     }); 
     begin = begin + interval; //update end recursion value 
     anim.start(); //begin the animation 
    } 
    super.end(); //this doesn't work... rip 
} 
+0

почему не использовать TimeAnimator? – pskink

ответ

0
public void start() 
{ 
    if(begin < end) //recursive end condition 
    { 
     int distance = interval + begin; //change the distance to be traveled 
     //create new animation to do part of the whole animation 
     ObjectAnimator anim = ObjectAnimator.ofFloat(toAnimate, property, begin,distance); 
     TimeInterpolator inter = new TimeInterpolator() //makes the animation move with only one frame 
     { 
      public float getInterpolation(float prog) 
      { 
       return Math.round(prog * 10)/10; 
      } 
     }; 
     anim.setInterpolator(inter); 
     anim.setDuration((long)(500)); 
     anim.addListener(new AnimatorListener() 
     { 
      @Override 
      public void onAnimationStart(Animator animation){} 
      @Override 
      public void onAnimationEnd(Animator animation) 
      { 
       begin += interval; 
       if(begin < end){ 
        start(); //start the next part of the movement 
       } 
       else{ 
        // Do something else 
       } 
      } 
      @Override 
      public void onAnimationCancel(Animator animation){} 
      @Override 
      public void onAnimationRepeat(Animator animation){} 
     }); 
     anim.start(); //begin the animation 
    } 
}