2015-04-22 94 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 
    } 
}