2015-04-22 88 views
0

我正在写一个“太空入侵者”风格的应用程序,我需要做的是经典的游戏移动停止重复动画。我使用下面的代码获得了进程的一种方式,但是当我想返回时,似乎并没有一种简单的方法来实现这一点。我认为最简单的方法是在结束动画事件中发生某些事情,但即使拨打super.end();,我也无法触发事件。我环顾四周寻找手动触发事件的方法,但无济于事。我这样做的方式可能有点粗略,但现在可以尝试使其一直工作。自定义动画结束触发器

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 
    } 
}