动画 -- 属性动画 -- 工作过程

源码解析

例子:ObjectAnimator.ofInt(mButton, "width", 500).setDuration(5000).start();

//ObjectAnimator#start:
public void start() {
    //如果当前动画、等待的动画和延迟的动画中有和当前动画相同的动画,就把相同的动画取消掉。
    //调用ValueAnimator的start方法。
}

//ValueAnimator#start:
private void start(boolean playBackwards) {
    if(Looper.myLooper() == null) { //属性动画需要运行在有Looper的线程中
        throw new AndroidRuntimeException("Animators may only be run on Looper threads");
    }
    ...
}

//ValueAnimator#doAnimationFrame:
final boolean doAnimationFrame(long frameTime) {
    ...
    return animationFrame(currentTime);
}

//-----对象属性的set方法被调用-----
//ObjectAnimator#animateValue:
void animateValue(float fraction) {
    ...
    super.animateValue(fraction); //调用ValueAnimator的animateValue方法,计算每帧动画所对应的属性值
    int numValues = mValues.length;
    for (int i = 0; i < numValues; ++i) {
        mValues[i].setAnimatedValue(target); //调用PropertyValuesHolder的setAnimatedValue方法,将新的属性值设置给对象,调用其set方法
    }
}

//ValueAnimator#animateValue:
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); //动画过程监听回调
        }
    }
}

//PropertyValuesHolder#setAnimatedValue:
void setAnimatedValue(Object target) { //将新的属性值设置给对象,调用其set方法
    if (mProperty != null) {
        mProperty.set(target, getAnimatedValue());
    }
    if (mSetter != null) {
        try {
            mTmpValueArray[0] = getAnimatedValue();
            mSetter.invoke(target, mTmpValueArray); //通过反射调用
        } catch (InvocationTargetException e) {
            Log.e("PropertyValuesHolder", e.toString());
        } catch (IllegalAccessException e) {
            Log.e("PropertyValuesHolder", e.toString());
        }
    }
}
//-----对象属性的set方法被调用-----

//-----对象属性的get方法被调用-----
//ObjectAnimator#setupStartValues:
public void setupStartValues() {
    initAnimation();

    final Object target = getTarget();
    if (target != null) {
        final int numValues = mValues.length;
            for (int i = 0; i < numValues; ++i) {
                mValues[i].setupStartValue(target);//调用PropertyValuesHolder的setupStartValue方法
            }
        }
}

//PropertyValuesHolder#setupStartValue:
void setupStartValue(Object target) {
    List keyframes = mKeyframes.getKeyframes();
    if (!keyframes.isEmpty()) {
        setupValue(target, keyframes.get(0)); //调用setupValue方法,初始化对象属性值,调用其get方法
    }
}

//PropertyValuesHolder#setupValue:
private void setupValue(Object target, Keyframe kf) { //初始化对象属性值,调用其get方法
        if (mProperty != null) {
            Object value = convertBack(mProperty.get(target));
            kf.setValue(value);
        } else {
            try {
                if (mGetter == null) {
                    Class targetClass = target.getClass();
                    setupGetter(targetClass);
                    if (mGetter == null) {
                        // Already logged the error - just return to avoid NPE
                        return;
                    }
                }
                Object value = convertBack(mGetter.invoke(target)); //通过反射调用
                kf.setValue(value);
            } catch (InvocationTargetException e) {
                Log.e("PropertyValuesHolder", e.toString());
            } catch (IllegalAccessException e) {
                Log.e("PropertyValuesHolder", e.toString());
            }
        }
}
//-----对象属性的get方法被调用-----

你可能感兴趣的:(动画 -- 属性动画 -- 工作过程)