2016-03-10 32 views
2

我正在嘗試使用ValueAnimator爲long值創建動畫。 ValueAnimator沒有.ofLong(long... values)構建器。達到此目的的最佳方法是什麼?將多頭投給int並不會給我我要找的結果。動畫長值

+0

你有沒有設法找到一個解決辦法?我目前面臨同樣的問題.. – RyanJohnstone

回答

0

我在將滾動位置創建爲基於滾動時間的視圖時遇到了同樣的問題,因爲滾動位置是毫秒長的值。我最終重構了使用秒數的int值,但如果這不適合你,這是我的原始解決方案。如果使用的是非常大的多頭,花車和鑄造回到多頭乘法可能導致不準確的,但給它一個鏡頭...

// There is a long somewhere that needs to be animated. Let's call it valueToAnimate 
    // This assumes it's a millisecond value for a time based scrolling view. 
    // Make a final copy of it for the animator 
    final long startValue = valueToAnimate; 
    // Also make a final reference to the target long for the animator 
    final long finishValue = valueToAnimate + 1000 * 60 * 60 * 24 * 14; // animate to 2 weeks from now 
    // Calculate distance needed to travel 
    final long distance = finishValue - startValue; 
    // Create a ValueAnimator of floats from 0 through to 1. 
    ValueAnimator animator = ValueAnimator.ofFloat(0, 1); 
    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
     @Override 
     public void onAnimationUpdate(ValueAnimator animation) { 
      long valueToAnimate = startValue + (long) (distance * (float) animation.getAnimatedValue()); 
     } 
    }); 
    animator.addListener(new Animator.AnimatorListener() { 
     @Override 
     public void onAnimationStart(Animator animation) { 

     } 

     @Override 
     public void onAnimationEnd(Animator animation) { 
      // Make sure you end on the correct value 
      valueToAnimate = finishValue; 
     } 

     @Override 
     public void onAnimationCancel(Animator animation) { 

     } 

     @Override 
     public void onAnimationRepeat(Animator animation) { 

     } 
    });