2016-11-21 156 views
0

我想暫停和恢復一個動畫設置(動畫設置中的所有動畫設置順序運行)。
我寫了這個代碼,但它不能正常工作(不來自同一個地方重新開始):暫停和恢復AnimatorSet Api <19

AnimatorSet animatorSet; 
    int startPosition = 0 ; 
    long playTime = 0; 
    public void playOrResume(Marker marker, List<LatLng> positions, final LatLngInterpolator latLngInterpolator) { 
     TypeEvaluator<LatLng> typeEvaluator = new TypeEvaluator<LatLng>() { 
      @Override 
      public LatLng evaluate(float fraction, LatLng startValue, LatLng endValue) { 
       return latLngInterpolator.interpolate(fraction, startValue, endValue); 
      } 
     }; 

     animatorSet = new AnimatorSet(); 
     Animator previous = null; 
     for (int i = startPosition ; startPosition < positions.size(); i++) { 
      LatLng finalPosition = positions.get(i); 
      Property<Marker, LatLng> property = Property.of(Marker.class, LatLng.class, "position"); 
      ObjectAnimator animator = ObjectAnimator.ofObject(marker, property, typeEvaluator, finalPosition); 
      if (previous != null) { 
       animatorSet.play(animator).after(previous); 
      } else { 
       animator.setCurrentPlayTime(playTime); 
       animatorSet.play(animator); 
      } 
      previous = animator; 
     } 
     animatorSet.setDuration(300); 
     animatorSet.start(); 
    } 

    public void pause() { 
     ArrayList<Animator> animators = animatorSet.getChildAnimations(); 
     for(int i= 0 ; i < animators.size() ; i++) { 
      Animator animator = animators.get(i); 
      if (animator.isStarted()){ 
       startPosition = i; 
       playTime = animator.getCurrentPlayTime(); 
       animatorSet.cancel(); 
       break; 
      } 
     } 
    } 

回答

2

假設在AnimatorSet動畫都是ValueAnimator/ObjectAnimators,您可以使用ValueAnimator.getCurrentPlayTime()來在取消之前追蹤正在進行的動畫製作者的播放位置。當您恢復設置時,您可以撥打ValueAnimator.setCurrentPlayTime(long)來恢復新設置中未完成的動畫製作者的播放位置。上面提到的兩種API是在API層面都推出了11

AnimatorSet animatorSet; 
    int startPosition = 0 ; 
    long playTime = 0; 
    public void playOrResume(Marker marker, List<LatLng> positions, final LatLngInterpolator latLngInterpolator) { 
     TypeEvaluator<LatLng> typeEvaluator = new TypeEvaluator<LatLng>() { 
      @Override 
      public LatLng evaluate(float fraction, LatLng startValue, LatLng endValue) { 
       return latLngInterpolator.interpolate(fraction, startValue, endValue); 
      } 
     }; 

     if (animatorSet == null) { 
      Animator previous = null; 
      for (int i = startPosition ; startPosition < positions.size(); i++) { 
       LatLng finalPosition = positions.get(i); 
       Property<Marker, LatLng> property = Property.of(Marker.class, LatLng.class, "position"); 
       ObjectAnimator animator = ObjectAnimator.ofObject(marker, property, typeEvaluator, finalPosition); 
       if (previous != null) { 
        animatorSet.play(animator).after(previous); 
       } else { 
        animatorSet.play(animator); 
       } 
       previous = animator; 
      } 
     } else { 
      // Reuse the old animators so the start values are not changed. 
      List<Animator> anims = animatorSet.getChildAnimations().subList(startPosition, positions.size() - 1); 
      // Restore the play time of the first animator. 
      anims.get(0).setCurrentPlayTime(playTime); 
      animatorSet = new AnimatorSet(); 
      animatorSet.playSequentially(anims); 
     } 
     animatorSet.setDuration(300); 
     animatorSet.start(); 
    } 

    public void pause() { 
     ArrayList<Animator> animators = animatorSet.getChildAnimations(); 
     for(int i= 0 ; i < animators.size() ; i++) { 
      Animator animator = animators.get(i); 
      if (animator.isStarted()){ 
       startPosition = i; 
       playTime = animator.getCurrentPlayTime(); 
       animatorSet.cancel(); 
       break; 
      } 
     } 
    } 
+0

@AHoneyBustard它不只是一個動畫師,這是一個AnimatorSet – ReZa

+0

可以,但只能有一個持續的在這個動畫設定任何給定的時間動畫。你需要找到那個,並保存/恢復播放時間,所以簡歷是無縫的。 @Reza –

+0

@AHoneyBustard @AHoneyBustard我加了吧,還是一樣的結果 – ReZa