Android属性动画源码解析

版权声明:本文为博主石月的原创文章,转载请注明出处 https://blog.csdn.net/liuxingrong666/article/details/84028609

属性动画是Android开发中常用的知识,网上大多数文章都是基于属性动画的用法来解说的,本文基于属性动画的源码,从源码角度去一探属性动画的究竟。

属性动画有两个非常重要的类,分别是ObjectAnimator和ValueAnimator,其中前者继承了后者,前者是对属性动画的api提供了更好的一层封装,方便开发者使用。属性动画常见的用法如下:

ObjectAnimator animator = ObjectAnimator.ofFloat(targetView, "translationX", 100);

animator.setInterpolator(new LinearInterpolator());
animator.setEvaluator(new FloatEvaluator());
animator.setDuration(100);
animator.start();

上面涉及到两个概念,插值器Interpolator和估值器Evaluator,插值器表示动画播放过程中的某个时间点播放进度的百分比,是用来控制播放速率的,估值器用来计算某个播放进度点需要改变的view的属性值,Evaluator.evaluate(float fraction, T startValue, T endValue) 是核心方法,fraction是进度百分比,startValue 和 endValue 表示动画的起始值和结束值,通过这个三个值去计算对应的view的属性值。

下面我们就基于上面的使用方式去追踪分析一下源码,首先点进ofFloat方法如下:

public static ObjectAnimator ofFloat(Object target, String propertyName, float... values) {
        ObjectAnimator anim = new ObjectAnimator(target, propertyName);
        anim.setFloatValues(values);
        return anim;
    }


//new ObjectAnimator代码如下
private ObjectAnimator(Object target, String propertyName) {
        setTarget(target);
        setPropertyName(propertyName);
    }


//setFloatValues方法的代码如下
    @Override
    public void setFloatValues(float... values) {
        if (mValues == null || mValues.length == 0) {
            // No values yet - this animator is being constructed piecemeal. Init the values with
            // whatever the current propertyName is
            if (mProperty != null) {
                setValues(PropertyValuesHolder.ofFloat(mProperty, values));
            } else {
                setValues(PropertyValuesHolder.ofFloat(mPropertyName, values));
            }
        } else {
            super.setFloatValues(values);
        }
    }

可以看到首先创建一个ObjectAnimator对象,进行动画view和动画属性名字的设置,然后通过setFloatValues设置我们一个动画值。上面出现了一个PropertyValuesHolder.ofFloat方法,看起来像是将属性相关值保存在一个类中,我们进去看看:


 public static PropertyValuesHolder ofFloat(Property<?, Float> property, float... values) {
        return new FloatPropertyValuesHolder(property, values);
    }


//下一步代码
public FloatPropertyValuesHolder(Property property, float... values) {
            super(property);
            setFloatValues(values);
            if (property instanceof  FloatProperty) {
                mFloatProperty = (FloatProperty) mProperty;
            }
        }


//setFloatValues的代码
public void setFloatValues(float... values) {
            super.setFloatValues(values);
            mFloatKeyframes = (Keyframes.FloatKeyframes) mKeyframes;
        }

//------
public void setFloatValues(float... values) {
        mValueType = float.class;
        mKeyframes = KeyframeSet.ofFloat(values);
    }


我们可以看到FloatPropertyValuesHolder是用来保存相关属性值的,其中出现了一个FloatKeyframes类,我们去看看这个类:

 // 通过这个方法去保存我们设置的属性values
  public static KeyframeSet ofFloat(float... values) {
        boolean badValue = false;
        int numKeyframes = values.length;
        FloatKeyframe keyframes[] = new FloatKeyframe[Math.max(numKeyframes,2)];
        if (numKeyframes == 1) {
            keyframes[0] = (FloatKeyframe) Keyframe.ofFloat(0f);
            keyframes[1] = (FloatKeyframe) Keyframe.ofFloat(1f, values[0]);
            if (Float.isNaN(values[0])) {
                badValue = true;
            }
        } else {
            keyframes[0] = (FloatKeyframe) Keyframe.ofFloat(0f, values[0]);
            for (int i = 1; i < numKeyframes; ++i) {
                keyframes[i] =
                        (FloatKeyframe) Keyframe.ofFloat((float) i / (numKeyframes - 1), values[i]);
                if (Float.isNaN(values[i])) {
                    badValue = true;
                }
            }
        }
        if (badValue) {
            Log.w("Animator", "Bad value (NaN) in float animator");
        }
        return new FloatKeyframeSet(keyframes);
    }

  在KeyframeSet的ofFloat方法中,我们创建一个FloatKeyframes数组,将属性values保存在数组中。其中Keyframe我们翻译叫关键帧,代表动画执行过程中的一些重要的帧,比如开始、结束、中间值等等。这些值在动画执行过程中都会用到。

到这里分析完setValues(PropertyValuesHolder.ofFloat(mProperty, values))中的PropertyValuesHolder.ofFloat方法的作用,下面来看看setValues方法:

 public void setValues(PropertyValuesHolder... values) {
        int numValues = values.length;
        mValues = values;
        mValuesMap = new HashMap<String, PropertyValuesHolder>(numValues);
        for (int i = 0; i < numValues; ++i) {
            PropertyValuesHolder valuesHolder = values[i];
            mValuesMap.put(valuesHolder.getPropertyName(), valuesHolder);
        }
        // New property/values/target should cause re-initialization prior to starting
        mInitialized = false;
    }

我们这里对mValues进行了赋值,这个值后面会经常用到,然后将属性holder保存到hashmap中,其中key是属性名。到这里我们分析完了ObjectAnimator.ofFloat(targetView, "translationX", 100);方法的整个过程,其实就是将我们设置的属性值保存起来。然后还有插值器和估值器,代码如下

//保存插值器 
@Override
    public void setInterpolator(TimeInterpolator value) {
        if (value != null) {
            mInterpolator = value;
        } else {
            mInterpolator = new LinearInterpolator();
        }
    }


//保存估值器
public void setEvaluator(TypeEvaluator value) {
        if (value != null && mValues != null && mValues.length > 0) {
            mValues[0].setEvaluator(value);
        }
    }

其中插值器是保存在ValueAnimator中,而估值器是保存在PropertyValuesHolder中。

上面属性值都已经设置好了,开始进入start方法,这个方法里面代码非常复杂,我们尽量简要一点说明吧,做好准备进入start阶段,先来看看start进入的关键代码:

 private void start(boolean playBackwards) {
        if (Looper.myLooper() == null) {
            throw new AndroidRuntimeException("Animators may only be run on Looper threads");
        }
        mReversing = playBackwards;
        mSelfPulse = !mSuppressSelfPulseRequested;
        // 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;
        mAnimationEndRequested = 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 = -1;
        mFirstFrameTime = -1;
        mStartTime = -1;
        //关键一
        addAnimationCallback(0);

        if (mStartDelay == 0 || mSeekFraction >= 0 || mReversing) {
            // 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);
            }
        }
    }

我们从中挑出三个关键的方法逐一分析:

1.addAnimationCallback(0);

2.startAnimation();

3.setCurrentPlayTime(0);

下面我们来看看addAnimationCallback方法,

private void addAnimationCallback(long delay) {
        if (!mSelfPulse) {
            return;
        }
        getAnimationHandler().addAnimationFrameCallback(this, delay);
    }


//AnimationHandler的addAnimationFrameCallback方法代码
   public void addAnimationFrameCallback(final AnimationFrameCallback callback, long delay) {
        if (mAnimationCallbacks.size() == 0) {
            //程序首先会执行这里,因为此时mAnimationCallbacks中还没有添加元素
            getProvider().postFrameCallback(mFrameCallback);
        }
        if (!mAnimationCallbacks.contains(callback)) {
            mAnimationCallbacks.add(callback);
        }

        if (delay > 0) {
            mDelayedCallbackStartTime.put(callback, (SystemClock.uptimeMillis() + delay));
        }
    }

这里会调用AnimationHandler的添加监听方法,其中又会转到getProvider().postFrameCallback(mFrameCallback)的执行,我们先来看看那个mFrameCallback这个参数,再去追踪getProvider方法

 private final Choreographer.FrameCallback mFrameCallback = new Choreographer.FrameCallback() {
        @Override
        public void doFrame(long frameTimeNanos) {
            doAnimationFrame(getProvider().getFrameTime());
            if (mAnimationCallbacks.size() > 0) {
                //重复调用
                getProvider().postFrameCallback(this);
            }
        }
    };

这个回调接口中,我们发现一个有趣的地方,就是会重复调用getProvider().postFrameCallback,这种反复操作的执行,有点像动画的绘制了。那我们先转去看看getProvider方法:

private AnimationFrameCallbackProvider getProvider() {
        if (mProvider == null) {
            mProvider = new MyFrameCallbackProvider();
        }
        return mProvider;
    }



   //其中MyFrameCallbackProvider类的代码
   final Choreographer mChoreographer = Choreographer.getInstance();

        @Override
        public void postFrameCallback(Choreographer.FrameCallback callback) {
            mChoreographer.postFrameCallback(callback);
        }

这里出现了一个新类Choreographer,我们去看看:

//Choreographer类的方法
public void postFrameCallback(FrameCallback callback) {
        postFrameCallbackDelayed(callback, 0);
    }


  //往下走
  public void postFrameCallbackDelayed(FrameCallback callback, long delayMillis) {
        if (callback == null) {
            throw new IllegalArgumentException("callback must not be null");
        }

        postCallbackDelayedInternal(CALLBACK_ANIMATION,
                callback, FRAME_CALLBACK_TOKEN, delayMillis);
    }


    //往下走
    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);
            }
        }
    }

程序又走了几步,经过判断,我们的代码会走到scheduleFrameLocked(now),下面我们进去看看:

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

上面代码有点多,我们只看程序执行的部分,上面的会通过mHandler发送一个MSG_DO_SCHEDULE_VSYNC消息,所以我们又去看看handler是怎么处理这个消息的:

   private final class FrameHandler extends Handler {
        public FrameHandler(Looper looper) {
            super(looper);
        }

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_DO_FRAME:
                    doFrame(System.nanoTime(), 0);
                    break;
                case MSG_DO_SCHEDULE_VSYNC:
                    doScheduleVsync();
                    break;

                //这里
                case MSG_DO_SCHEDULE_CALLBACK:
                    doScheduleCallback(msg.arg1);
                    break;
            }
        }
    }


   //下一步
   void doScheduleCallback(int callbackType) {
        synchronized (mLock) {
            if (!mFrameScheduled) {
                final long now = SystemClock.uptimeMillis();
                if (mCallbackQueues[callbackType].hasDueCallbacksLocked(now)) {
                    scheduleFrameLocked(now);
                }
            }
        }
    }

通过上面可以看到代码会执行到scheduleFrameLocked(now)方法,进去看看:

private void scheduleFrameLocked(long now) {
    if (!mFrameScheduled) {
        mFrameScheduled = true;
        if (USE_VSYNC) {
            ……
            if (isRunningOnLooperThreadLocked()) {
                scheduleVsyncLocked();
            } else {
                Message msg = mHandler.obtainMessage(MSG_DO_SCHEDULE_VSYNC);
                msg.setAsynchronous(true);
                mHandler.sendMessageAtFrontOfQueue(msg);
            }
        }
        ……
    }
}

上面的代码会执行到scheduleVsyncLocked()方法,

private void scheduleVsyncLocked() {
        mDisplayEventReceiver.scheduleVsync();
    }


  //其中DisplayEventReceiver类的代码
  public void scheduleVsync() {
        if (mReceiverPtr == 0) {
            Log.w(TAG, "Attempted to schedule a vertical sync pulse but the display event "
                    + "receiver has already been disposed.");
        } else {
            nativeScheduleVsync(mReceiverPtr);
        }
    }

到这里的时候,调用了一个jni方法,呵呵!其中参数mReceiverPtr是一个 jni 层指向 DisplayEventReceiver(子类 FrameDisplayEventReceiver) 的指针,jni 方法会回调 FrameDisplayEventReceiver.onVsync() 方法,我们姑且不探究jni层是如何实现的,继续看onVsync方法的代码:

    @Override
        public void onVsync(long timestampNanos, int builtInDisplayId, int frame) {

            //。。。。。省略代码

            mTimestampNanos = timestampNanos;
            mFrame = frame;
            Message msg = Message.obtain(mHandler, this);
            msg.setAsynchronous(true);
            mHandler.sendMessageAtTime(msg, timestampNanos / TimeUtils.NANOS_PER_MS);

            }

这里通过 mHandler 调用自身 FrameDisplayEventReceiver.run()方法,

public void run() {
            mHavePendingVsync = false;
            doFrame(mTimestampNanos, mFrame);
        }



//doFrame的代码
void doFrame(long frameTimeNanos, int frame) {
    final long startNanos;
    synchronized (mLock) {
        ……

    try {
        Trace.traceBegin(Trace.TRACE_TAG_VIEW, "Choreographer#doFrame");
        AnimationUtils.lockAnimationClock(frameTimeNanos / TimeUtils.NANOS_PER_MS);

        mFrameInfo.markInputHandlingStart();
        doCallbacks(Choreographer.CALLBACK_INPUT, frameTimeNanos);

        mFrameInfo.markAnimationsStart();
        doCallbacks(Choreographer.CALLBACK_ANIMATION, frameTimeNanos);

        mFrameInfo.markPerformTraversalsStart();
        doCallbacks(Choreographer.CALLBACK_TRAVERSAL, frameTimeNanos);

        doCallbacks(Choreographer.CALLBACK_COMMIT, frameTimeNanos);
    } finally {
        AnimationUtils.unlockAnimationClock();
        Trace.traceEnd(Trace.TRACE_TAG_VIEW);
    }

    ……
}

我们可以看到开始执行各种doCallbacks方法了,这个是关键,下面快点进入去看看

void doCallbacks(int callbackType, long frameTimeNanos) {
        CallbackRecord callbacks;
        synchronized (mLock) {
         
            final long now = System.nanoTime();
            //获取我们之前保存的callbacks
            callbacks = mCallbackQueues[callbackType].extractDueCallbacksLocked(
                    now / TimeUtils.NANOS_PER_MS);
            if (callbacks == null) {
                return;
            }
            mCallbacksRunning = true;

    //。。。。。。。。。。省略代码

        try {
            Trace.traceBegin(Trace.TRACE_TAG_VIEW, CALLBACK_TRACE_TITLES[callbackType]);
            for (CallbackRecord c = callbacks; c != null; c = c.next) {
                if (DEBUG_FRAMES) {
                    Log.d(TAG, "RunCallback: type=" + callbackType
                            + ", action=" + c.action + ", token=" + c.token
                            + ", latencyMillis=" + (SystemClock.uptimeMillis() - c.dueTime));
                }
                //执行callback
                c.run(frameTimeNanos);
            }
        } finally {
            synchronized (mLock) {
                mCallbacksRunning = false;
                do {
                    final CallbackRecord next = callbacks.next;
                    recycleCallbackLocked(callbacks);
                    callbacks = next;
                } while (callbacks != null);
            }
            Trace.traceEnd(Trace.TRACE_TAG_VIEW);
        }
    }

其中我们会获取到之前保存的各种callback,然后通过run方法执行callback,其中c就是mCallbackQueues在下面的方法中保存的元素

    private void postCallbackDelayedInternal(int callbackType,

        ...................

        synchronized (mLock) {
            final long now = SystemClock.uptimeMillis();
            final long dueTime = now + delayMillis;
            //在这里保存的
            mCallbackQueues[callbackType].addCallbackLocked(dueTime, action, token);

         ................... 

        }
    }

下面我们看看CallbackRecord 的run方法:

    public void run(long frameTimeNanos) {
            if (token == FRAME_CALLBACK_TOKEN) {
                ((FrameCallback)action).doFrame(frameTimeNanos);
            } else {
                ((Runnable)action).run();
            }
        }
    }

这里的action就是我们上面执行过的mFrameCallback这个参数,再看看下面的代码清晰一点,因为绕了太多了,到这里我们就知道了绘制动画的核心,因为这个回调会反复执行,这就达到了绘制的效果。下面我要去看看下面的doAnimationFrame方法了

 private final Choreographer.FrameCallback mFrameCallback = new Choreographer.FrameCallback() {
        @Override
        public void doFrame(long frameTimeNanos) {
            doAnimationFrame(getProvider().getFrameTime());
            if (mAnimationCallbacks.size() > 0) {
                //重复调用
                getProvider().postFrameCallback(this);
            }
        }
    };

代码:

 private void doAnimationFrame(long frameTime) {
        long currentTime = SystemClock.uptimeMillis();
        final int size = mAnimationCallbacks.size();
        for (int i = 0; i < size; i++) {
            final AnimationFrameCallback callback = mAnimationCallbacks.get(i);
            if (callback == null) {
                continue;
            }
            if (isCallbackDue(callback, currentTime)) {

                //关键点
                callback.doAnimationFrame(frameTime);
                if (mCommitCallbacks.contains(callback)) {
                    getProvider().postCommitCallback(new Runnable() {
                        @Override
                        public void run() {
                            commitAnimationFrame(callback, getProvider().getFrameTime());
                        }
                    });
                }
            }
        }
        cleanUpList();
    }

看关键callback.doAnimationFrame(frameTime),这个callback是ValueAnimator中的

getAnimationHandler().addAnimationFrameCallback(this, delay)

添加的,我们它所实现的接口方法:

 public final boolean doAnimationFrame(long frameTime) {
        
        ...........
        直接看这里
        boolean finished = animateBasedOnTime(currentTime);

        if (finished) {
            endAnimation();
        }
        return finished;
    }

我们继续看 animateBasedOnTime(currentTime)

boolean animateBasedOnTime(long currentTime) {
    boolean done = false;
    if (mRunning) {
        ……
        animateValue(currentIterationFraction);
    }
    return done;
}


//往下看
void 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);
        }
    }
}

在这里高兴地看到了mValues的调用,好像是要赋fraction的值,就是播放进度,后面还调用了onAnimationUpdate方法,我们知道在ValueAnimator中有一个动画更新的回调,好像这里找到了被调用的地方了

valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            ……
            ……
        }
});

刚刚看的是ValueAnimator的方法,下面我们去ObjectAnimator中的animateValue方法

    @Override
    void animateValue(float fraction) {
        final Object target = getTarget();
        if (mTarget != null && target == null) {
            // We lost the target reference, cancel and clean up. Note: we allow null target if the
            /// target has never been set.
            cancel();
            return;
        }

        super.animateValue(fraction);
        int numValues = mValues.length;
        for (int i = 0; i < numValues; ++i) {
            mValues[i].setAnimatedValue(target);
        }
    }

我们看到了mValues[i].setAnimatedValue(target)关键点,我们知道mValues中保存的是FloatPropertyValuesHolder 对象,就是我们所保存的各个属性值,下面我们去看看具体的方法实现

void setAnimatedValue(Object target) {
        if (mFloatProperty != null) {
            mFloatProperty.setValue(target, mFloatAnimatedValue);
            return;
        }
        if (mProperty != null) {
            mProperty.set(target, mFloatAnimatedValue);
            return;
        }
        if (mJniSetter != 0) {
            nCallFloatMethod(target, mJniSetter, mFloatAnimatedValue);
            return;
        }
        if (mSetter != null) {
            try {
                mTmpValueArray[0] = mFloatAnimatedValue;
                mSetter.invoke(target, mTmpValueArray);
            } catch (InvocationTargetException e) {
                Log.e("PropertyValuesHolder", e.toString());
            } catch (IllegalAccessException e) {
                Log.e("PropertyValuesHolder", e.toString());
            }
        }
    }

到这里有几个if判断,我们不知道程序会走哪一步,那我们先放一下,回到最前面,我们还有两个方法没追踪,下面我们来看看startAnimation()方法:

private void startAnimation() {
    if (Trace.isTagEnabled(Trace.TRACE_TAG_VIEW)) {
        Trace.asyncTraceBegin(Trace.TRACE_TAG_VIEW, getNameForTrace(),
                System.identityHashCode(this));
    }

    mAnimationEndRequested = false;
    initAnimation();
    mRunning = true;
    if (mSeekFraction >= 0) {
        mOverallFraction = mSeekFraction;
    } else {
        mOverallFraction = 0f;
    }
    if (mListeners != null) {
        notifyStartListeners();
    }
}

这里有一个初始化的initAnimation()方法,下面我们看看

void initAnimation() {
    if (!mInitialized) {
        int numValues = mValues.length;
        for (int i = 0; i < numValues; ++i) {
            mValues[i].init();
        }
        mInitialized = true;
    }
}


//看看 FloatPropertyValuesHolder.init()方法
void init() {
    if (mEvaluator == null) {
        // We already handle int and float automatically, but not their Object
        // equivalents
        mEvaluator = (mValueType == Integer.class) ? sIntEvaluator :
                (mValueType == Float.class) ? sFloatEvaluator :
                null;
    }
    if (mEvaluator != null) {
        // KeyframeSet knows how to evaluate the common types - only give it a custom
        // evaluator if one has been set on this class
        mKeyframes.setEvaluator(mEvaluator);
    }
}

上面我们调用到FloatPropertyValuesHolder.init()方法,好像没什么值得发现的,下面我们来看看ObjectAnimator的initAnimation方法:

void initAnimation() {
    if (!mInitialized) {
        // mValueType may change due to setter/getter setup; do this before calling super.init(),
        // which uses mValueType to set up the default type evaluator.
        final Object target = getTarget();
        if (target != null) {
            final int numValues = mValues.length;
            for (int i = 0; i < numValues; ++i) {
                mValues[i].setupSetterAndGetter(target);
            }
        }
        super.initAnimation();
    }
}

会调用 mValues[i].setupSetterAndGetter(target) 即FloatPropertyValuesHolder.setupSetterAndGetter(target),继续跟进

void setupSetterAndGetter(Object target) {
        setupSetter(target.getClass());
    }


//继续
void setupSetter(Class targetClass) {
        if (mJniSetter != 0) {
            return;
        }
        synchronized(sJNISetterPropertyMap) {
            HashMap<String, Long> propertyMap = sJNISetterPropertyMap.get(targetClass);
            boolean wasInMap = false;
            if (propertyMap != null) {
                wasInMap = propertyMap.containsKey(mPropertyName);
                if (wasInMap) {
                    Long jniSetter = propertyMap.get(mPropertyName);
                    if (jniSetter != null) {
                        mJniSetter = jniSetter;
                    }
                }
            }
            if (!wasInMap) {
                String methodName = getMethodName("set", mPropertyName);
                calculateValue(0f);
                float[] values = (float[]) getAnimatedValue();
                int numParams = values.length;
                try {
                    mJniSetter = nGetMultipleFloatMethod(targetClass, methodName, numParams);
                } catch (NoSuchMethodError e) {
                    // try without the 'set' prefix
                    try {
                        mJniSetter = nGetMultipleFloatMethod(targetClass, mPropertyName,
                                numParams);
                    } catch (NoSuchMethodError e2) {
                        // just try reflection next
                    }
                }
                if (propertyMap == null) {
                    propertyMap = new HashMap<String, Long>();
                    sJNISetterPropertyMap.put(targetClass, propertyMap);
                }
                propertyMap.put(mPropertyName, mJniSetter);
            }
        }
    }
}

这里会对mJniSetter 进行赋值,所以回到上面几个if的判断中,我们可以知道,程序会走

 if (mJniSetter != 0) {
            nCallFloatMethod(target, mJniSetter, mFloatAnimatedValue);
            return;
        }

这里会通过jni指针,修改对应的属性参数,即view.setTranslanteX(value)方法。代码到这里,我们完成了view属性的设置,整个动画过程中,随着动画的进度不断改变,属性值也会不断改变,然后通过我们的设置,目标view的属性值也会不断改变,从而达到属性动画的效果。

下面我们看看最后第一个关键方法setCurrentPlayTime(0)

public void setCurrentPlayTime(long playTime) {
    float fraction = mDuration > 0 ? (float) playTime / mDuration : 1;
    setCurrentFraction(fraction);
}


// setCurrentPlayTime(0) 还是会调用 setCurrentFraction(mSeekFraction)
public void setCurrentFraction(float fraction) {
    initAnimation();
    fraction = clampFraction(fraction);
    mStartTimeCommitted = true; // do not allow start time to be compensated for jank
    if (isPulsingInternal()) {
        long seekTime = (long) (getScaledDuration() * fraction);
        long currentTime = AnimationUtils.currentAnimationTimeMillis();
        // Only modify the start time when the animation is running. Seek fraction will ensure
        // non-running animations skip to the correct start time.
        mStartTime = currentTime - seekTime;
    } else {
        // If the animation loop hasn't started, or during start delay, the startTime will be
        // adjusted once the delay has passed based on seek fraction.
        mSeekFraction = fraction;
    }
    mOverallFraction = fraction;
    final float currentIterationFraction = getCurrentIterationFraction(fraction, mReversing);
    animateValue(currentIterationFraction);
}

可以看到上面会调用到animateValue(currentIterationFraction)方法,我们知道这个方法就是对view属性的更新,所以可以知道在调用了startAnimation()之后,就会立即进行一次view的更新操作,此时参数fraction为0。

上面就是属性动画源码解析全过程了,实在是太长了,阅读源码我们还是要抓住关键的步骤,不能沉溺于不重要的细节,在开发路上我们共勉吧!

猜你喜欢

转载自blog.csdn.net/liuxingrong666/article/details/84028609