ObjectAnimator实现机制_源码分析

一.使用
入口1: ObjectAnimator anim = ObjectAnimator.ofInt(foo, "alpha", 1, 100);
入口2: anim.setDuration(1000);
anim.start();


二.实现
0.ObjectAnimator继承ValueAnimator


1.入口1: ObjectAnimator anim = ObjectAnimator.ofInt(foo, "alpha", 1, 100);
public static ObjectAnimator ofInt(Object target, String propertyName, int... values) {
ObjectAnimator anim = new ObjectAnimator(target, propertyName); // 1.
anim.setIntValues(values); // 2.
return anim;
}
private ObjectAnimator(Object target, String propertyName) { // 3.设置属性值和属性对应的对象
mTarget = target;
setPropertyName(propertyName);
}

public void setPropertyName(String propertyName) {
// mValues could be null if this is being constructed piecemeal. Just record the
// propertyName to be used later when setValues() is called if so.
if (mValues != null) {
PropertyValuesHolder valuesHolder = mValues[0];
String oldName = valuesHolder.getPropertyName();
valuesHolder.setPropertyName(propertyName);
mValuesMap.remove(oldName);
mValuesMap.put(propertyName, valuesHolder);
}
mPropertyName = propertyName; // 记录属性名
}

@Override
public void setIntValues(int... values) { // 4.绑定属性和属性对应的一系列值到PropertyValuesHolder
setValues(PropertyValuesHolder.ofInt(mPropertyName, values)); // 工厂:创建一个IntPropertyValuesHolder
}

public static PropertyValuesHolder ofInt(Property property, int... values) {
return new IntPropertyValuesHolder(property, values);
}

public void setValues(PropertyValuesHolder... values) {
int numValues = values.length;
mValues = values; // 实质只有[0]有值,其他都为空
mValuesMap = new HashMap(numValues);
for (int i = 0; i < numValues; ++i) { // numValues值为1
PropertyValuesHolder valuesHolder = values[i];
mValuesMap.put(valuesHolder.getPropertyName(), valuesHolder); // 实质只有[0]有值,其他都为空
}
// New property/values/target should cause re-initialization prior to starting
mInitialized = false;
}



2.入口2: anim.setDuration(1000);
anim.start(); // 大部分工作都在ValueAnimator中完成,只有修改属性值在ObjectAnimator中实现

@Override
public void start() {
super.start();
}

... // 一系列调用过程参考"ValueAnimator实现机制"


// 一系列动画的过程:最终回调这个函数改变动画的值
@Override
void animateValue(float fraction) {
// 1.
super.animateValue(fraction);

int numValues = mValues.length;
for (int i = 0; i < numValues; ++i) {
// 3.核心代码:改变属性值
mValues[i].setAnimatedValue(mTarget); // mTarget:构造的时候传递进来的要改变属性的对象
}
}

void setAnimatedValue(Object target) {
...
if (mSetter != null) { // 属性对应的set方法
mTmpValueArray[0] = getAnimatedValue();
mSetter.invoke(target, mTmpValueArray);
}
}

void calculateValue(float fraction) { // 先计算出动画过程属性
mAnimatedValue = https://www.it610.com/article/mKeyframeSet.getValue(fraction);
}

Object getAnimatedValue() { // 获取动画过程属性值
return mAnimatedValue;
}

// 2.父类animateValue
// ValueAnimtor.java
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); // 计算出fraction对应的属性值(根据插值器)
}
if (mUpdateListeners != null) {
int numListeners = mUpdateListeners.size();
for (int i = 0; i < numListeners; ++i) {
mUpdateListeners.get(i).onAnimationUpdate(this);
}
}
}


三.总结
1.ObjectAnimator == ValueAnimator+动画过程反射改变属性的值(在animateValue中执行)

















    推荐阅读