分析Android屬性動畫源碼的執行過程

我們從啟動動畫為著手點分析屬性動畫的執行過程

@Override
public void start() {    
  AnimationHandler.getInstance().autoCancelBasedOn(this);    
  if (DBG) {        
    Log.d(LOG_TAG, "Anim target, duration: " + getTarget() + ", " + getDuration());        
    for (int i = 0; i < mValues.length; ++i) {            
      PropertyValuesHolder pvh = mValues[i];            
      Log.d(LOG_TAG, "Values[" + i + "]: " + pvh.getPropertyName() + ", " + pvh.mKeyframes.getValue(0) + ", " + pvh.mKeyframes.getValue(1));        
    }    
  }    
  super.start();
}

該段代碼只需看第一行和最后一行,第一行代碼為查看ThreadLocal中是否有AnimationHandler實例,并為其賦初值,如果mAnimationCallbacks這個ArrayList中有保存回調,那么遍歷該ArrayList并查看元素是否應該自動取消,最后一行調用了父類的start方法,那么我們就查看下ValueAnimator中的start方法

@Overridepublic void start() {    
  start(false);
}
private void start(boolean playBackwards) {
  if (Looper.myLooper() == null) {        
      throw new AndroidRuntimeException("Animators may only be run on Looper threads");    
  }    
  mReversing = playBackwards;    
  // Special case: reversing from seek-to-0 should act as if not seeked at all.    
  if (playBackwards && mSeekFraction != -1 && mSeekFraction != 0) {
      if (mRepeatCount == INFINITE) {            
          // Calculate the fraction of the current iteration.            
          float fraction = (float) (mSeekFraction - Math.floor(mSeekFraction));
          mSeekFraction = 1 - fraction;        
      } else {
          mSeekFraction = 1 + mRepeatCount - mSeekFraction;
      }    
  }    
  mStarted = true;    
  mPaused = false;    
  mRunning = false;    
  // Resets mLastFrameTime when start() is called, so that if the animation was running,    
  // calling start() would put the animation in the    
  // started-but-not-yet-reached-the-first-frame phase.    
  mLastFrameTime = 0;    
  AnimationHandler animationHandler = AnimationHandler.getInstance();
  animationHandler.addAnimationFrameCallback(this, (long) (mStartDelay * sDurationScale));    
  if (mStartDelay == 0 || mSeekFraction >= 0) {        
    // If there's no start delay, init the animation and notify start listeners right away        
      // to be consistent with the previous behavior. Otherwise, postpone this until the first        
      // frame after the start delay.        
      startAnimation();        
      if (mSeekFraction == -1) {            
      // No seek, start at play time 0. Note that the reason we are not using fraction 0            
      // is because for animations with 0 duration, we want to be consistent with pre-N           
      // behavior: skip to the final value immediately.            
        setCurrentPlayTime(0);        
      } else {            
          setCurrentFraction(mSeekFraction);        
      }    
  }
}

可以看出動畫時運行在有Looper中的線程中的(為什么要在Looper中因為之后的代碼使用到了Handler),我們看到
animationHandler.addAnimationFrameCallback(this, (long) (mStartDelay * sDurationScale));讓我們走進這行代碼深究一下

public void addAnimationFrameCallback(final AnimationFrameCallback callback, long delay) {    
    if (mAnimationCallbacks.size() == 0) {
        getProvider().postFrameCallback(mFrameCallback);    
    }    
    if (!mAnimationCallbacks.contains(callback)) {
        mAnimationCallbacks.add(callback);    
    }    
    if (delay > 0) {        
        mDelayedCallbackStartTime.put(callback, (SystemClock.uptimeMillis() + delay));    
    }
}
getProvider()返回的是MyFrameCallbackProvider的實例,而MyFrameCallbackProvider由實現了AnimationFrameCallbackProvider接口,所以讓我們愉快的查看MyFrameCallbackProvider中的postFrameCallback方法
@Overridepublic void postFrameCallback(Choreographer.FrameCallback callback) {  
    mChoreographer.postFrameCallback(callback);
}

該方法內部逐層調用最終調用了

private void postCallbackDelayedInternal(int callbackType, Object action, Object token, long delayMillis) {    
    if (DEBUG_FRAMES) {        
        Log.d(TAG, "PostCallback: type=" + callbackType + ", action=" + action + ", token=" + token + ", delayMillis=" + delayMillis);    
    }    
    synchronized (mLock) {        
        final long now = SystemClock.uptimeMillis();        
        final long dueTime = now + delayMillis;
        mCallbackQueues[callbackType].addCallbackLocked(dueTime, action, token);       
        if (dueTime <= now) {           
           scheduleFrameLocked(now);       
        } else {           
           Message msg = mHandler.obtainMessage(MSG_DO_SCHEDULE_CALLBACK, action);         
           msg.arg1 = callbackType;   
           msg.setAsynchronous(true); 
           mHandler.sendMessageAtTime(msg, dueTime);    
        }  
    }
}

我們看到這里使用到了Handler,所以這就解釋了為什么前面需要檢查Looper是否為空了。我們可以看到邏輯是如果有延遲那么就運行else方法,沒有設置延遲都是調用scheduleFrameLocked方法

private void scheduleFrameLocked(long now) {    
   if (!mFrameScheduled) {        
      mFrameScheduled = true;        
      if (USE_VSYNC) {            
          if (DEBUG_FRAMES) {                
              Log.d(TAG, "Scheduling next frame on vsync.");           
          }           
          // If running on the Looper thread, then schedule the vsync immediately,
         // otherwise post a message to schedule the vsync from the UI thread
        // as soon as possible.
          if (isRunningOnLooperThreadLocked()) {
              scheduleVsyncLocked();
          } else {
              Message msg = mHandler.obtainMessage(MSG_DO_SCHEDULE_VSYNC);
              msg.setAsynchronous(true); 
              mHandler.sendMessageAtFrontOfQueue(msg);
          }
      } else {
          final long nextFrameTime = Math.max(mLastFrameTimeNanos / TimeUtils.NANOS_PER_MS + sFrameDelay, now);
          if (DEBUG_FRAMES) {
              Log.d(TAG, "Scheduling next frame in " + (nextFrameTime - now) + " ms.");
          }
          Message msg = mHandler.obtainMessage(MSG_DO_FRAME);            
          msg.setAsynchronous(true);
          mHandler.sendMessageAtTime(msg, nextFrameTime);
      }
   }
}

我們看出最后運行的是scheduleVsyncLocked();方法,而該方法內部調用的是native方法。因為之前調用傳遞的是Choreographer.FrameCallback實例,所以最終因該會調用該實例的doFrame方法,而會調用AnimationHandler的doAnimationFrame方法,而該方法又會調用ValueAnimator的doAnimationFrame方法

public final void doAnimationFrame(long frameTime) {
    AnimationHandler handler = AnimationHandler.getInstance();
    if (mLastFrameTime == 0) {
        // First frame
        handler.addOneShotCommitCallback(this);
        if (mStartDelay > 0) {
            startAnimation();
        }
        if (mSeekFraction < 0) {
            mStartTime = frameTime;
        } else {
            long seekTime = (long) (getScaledDuration() * mSeekFraction);            mStartTime = frameTime - seekTime;
            mSeekFraction = -1;
        }
        mStartTimeCommitted = false; // allow start time to be compensated for jank
    }
    mLastFrameTime = frameTime;
    if (mPaused) {
        mPauseTime = frameTime;
        handler.removeCallback(this);
        return;
    } else if (mResumed) {
        mResumed = false;
        if (mPauseTime > 0) {
            // Offset by the duration that the animation was paused
            mStartTime += (frameTime - mPauseTime);
            mStartTimeCommitted = false; // allow start time to be compensated for jank
        }
        handler.addOneShotCommitCallback(this);
    }
    // The frame time might be before the start time during the first frame of
    // an animation.  The "current time" must always be on or after the start
    // time to avoid animating frames at negative time intervals.  In practice, this
    // is very rare and only happens when seeking backwards.
    final long currentTime = Math.max(frameTime, mStartTime);
    boolean finished = animateBasedOnTime(currentTime);
    if (finished) {
        endAnimation();
    }
}

我們看到從animateBasedOnTime(currentTime);方法的返回值用于判斷是否結束動畫

boolean animateBasedOnTime(long currentTime) {
    boolean done = false;
    if (mRunning) {
        final long scaledDuration = getScaledDuration();
        final float fraction = scaledDuration > 0 ? (float)(currentTime - mStartTime) / scaledDuration : 1f;
        final float lastFraction = mOverallFraction;
        final boolean newIteration = (int) fraction > (int) lastFraction;
        final boolean lastIterationFinished = (fraction >= mRepeatCount + 1) && (mRepeatCount != INFINITE);
        if (scaledDuration == 0) {
            // 0 duration animator, ignore the repeat count and skip to the end
            done = true;
        } else if (newIteration && !lastIterationFinished) {
            // Time to repeat
            if (mListeners != null) {
                int numListeners = mListeners.size();
                for (int i = 0; i < numListeners; ++i) {
                    mListeners.get(i).onAnimationRepeat(this);
                }
            }
        } else if (lastIterationFinished) {
            done = true;
        }
        mOverallFraction = clampFraction(fraction);
        float currentIterationFraction = getCurrentIterationFraction(mOverallFraction);
        animateValue(currentIterationFraction);
    }
    return done;
}
@CallSupervoid animateValue(float fraction) {
    fraction = mInterpolator.getInterpolation(fraction);
    mCurrentFraction = fraction;
    int numValues = mValues.length;
    for (int i = 0; i < numValues; ++i) {
        mValues[i].calculateValue(fraction);
    }
    if (mUpdateListeners != null) {
        int numListeners = mUpdateListeners.size();
        for (int i = 0; i < numListeners; ++i) {
            mUpdateListeners.get(i).onAnimationUpdate(this);
        }
    }
}

我們看到calculateValue(fraction);方法使用PropertyValuesHolder中的calculateValue去計算值。

未完善,待續。。。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容