属性动画机制原理解析

移动开发 Android
动画的使用是 Android 开发中常用的知识,可是动画的种类繁多、使用复杂,每当需要采用自定义动画 实现 复杂的动画效果时,很多开发者就显得束手无策。

[[437959]]

本文转载自微信公众号「Android开发编程」,作者Android开发编程 。转载本文请联系Android开发编程公众号。

前言

动画的使用是 Android 开发中常用的知识

可是动画的种类繁多、使用复杂,每当需要采用自定义动画 实现 复杂的动画效果时,很多开发者就显得束手无策;

今天我们就来从源码中分析属性动画原理

一、动画简单应用

ValueAnimator

属性动画的最核心的类,原理:控制值的变化,之后手动赋值给对象的属性,从而实现动画;

对于控制的值的不同,Android 提供给我们三种构造方法来实例ValueAnimator对象:

ValueAnimator.ofInt(int... values) -- 整型数值

ValueAnimator.ofFloat(float... values) -- 浮点型数值

ValueAnimator.ofObject(TypeEvaluator evaluator, Object... values) -- 自定义对象类型

1、java方式

  1. //设置动画 始 & 末值 
  2.                 //ofInt()两个作用: 
  3.                 //1. 获取实例 
  4.                 //2. 在传入参数之间平滑过渡 
  5.                 //如下则0平滑过渡到3 
  6.                 ValueAnimator animator = ValueAnimator.ofInt(0,3); 
  7.                 //如下传入多个参数,效果则为0->5,5->3,3->10 
  8.                 //ValueAnimator animator = ValueAnimator.ofInt(0,5,3,10); 
  9.                 //设置动画的基础属性 
  10.                 animator.setDuration(5000);//播放时长 
  11.                 animator.setStartDelay(300);//延迟播放 
  12.                 animator.setRepeatCount(0);//重放次数 
  13.                 animator.setRepeatMode(ValueAnimator.RESTART); 
  14.                 //重放模式 
  15.                 //ValueAnimator.START:正序 
  16.                 //ValueAnimator.REVERSE:倒序 
  17.                 //设置更新监听 
  18.                 //值 改变一次,该方法就执行一次 
  19.                 animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
  20.                     @Override 
  21.                     public void onAnimationUpdate(ValueAnimator animation) { 
  22.                         //获取改变后的值 
  23.                         int currentValue = (int) animation.getAnimatedValue(); 
  24.                         //输出改变后的值 
  25.                         Log.d("test""onAnimationUpdate: " + currentValue); 
  26.                         //改变后的值发赋值给对象的属性值 
  27.                         view.setproperty(currentValue); 
  28.                         //刷新视图 
  29.                         view.requestLayout(); 
  30.                     } 
  31.                 }); 
  32.                 //启动动画 
  33.                 animator.start(); 

2、 XML 方式

在路径 res/animator/ 路径下常见 XML 文件,如 set_animator.xml

在上述文件中设置动画参数

  1. // ValueAnimator采用<animator>  标签 
  2. <animator xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:duration="1000" 
  4.     android:valueFrom="1" 
  5.     android:valueTo="0" 
  6.     android:valueType="floatType" 
  7.     android:repeatCount="1" 
  8.     android:repeatMode="reverse"/> 
  9. /> 

Java代码启动动画

  1. Animator animator = AnimatorInflater.loadAnimator(context, R.animator.set_animation);   
  2. // 载入XML动画 
  3. animator.setTarget(view);   
  4. // 设置动画对象 
  5. animator.start();   

二、原理详解

1、创建动画

  1. ObjectAnimator.ofFloat()开始; 
  2.     /** 
  3.      * 构建一个返回值为 float 的 ObjectAnimator 的实例 
  4.      * 
  5.      * @param target 作用于动画的对象。 
  6.      * @param propertyName 属性名称,要求对象须有setXXX() 方法,且是 public 的。 
  7.      * @param values,属性变化的值,可以设置  1 个或者 多个。当只有 1 个时,起始值为属性值本身。当有 2 个值时,第 1 个为起始值,第 2 个为终止值。当超过 2 个时,首尾值的定义与 2 个时一样,中间值做需要经过的值。 
  8.      */ 
  9. public static ObjectAnimator ofFloat(Object target, String propertyName, float... values) { 
  10.         ObjectAnimator anim = new ObjectAnimator(target, propertyName); 
  11.         anim.setFloatValues(values); 
  12.         return anim; 
  13.     } 
  • 创建一个 ObjectAnimator 的实例,然后为该实例设置 values;
  • 那么,继续看 ObjectAnimator 的构建;

构造 ObjectaAnimator

  1. private ObjectAnimator(Object target, String propertyName) { 
  2.         setTarget(target); 
  3.         setPropertyName(propertyName); 
  4.     } 

分别调用了 setTarget() 方法和setPropertyName();

2、setTarget()

  1. public void setTarget(@Nullable Object target) { 
  2.         final Object oldTarget = getTarget(); 
  3.         if (oldTarget != target) { 
  4.             if (isStarted()) { 
  5.                 cancel(); 
  6.             } 
  7.             mTarget = target == null ? null : new WeakReference<Object>(target); 
  8.             // New target should cause re-initialization prior to starting 
  9.             mInitialized = false
  10.         } 
  11.     } 

存在旧动画对象(也可为 null) 与新设置的动画对象不一致;

如果旧动画是开始了的状态,则先取消动画,然后将动画对象以弱引用对象为记录下来;

3、setPropertyName()

  1. public void setPropertyName(@NonNull String propertyName) { 
  2.         // mValues could be null if this is being constructed piecemeal. Just record the 
  3.         // propertyName to be used later when setValues() is called if so. 
  4.         if (mValues != null) { 
  5.             PropertyValuesHolder valuesHolder = mValues[0]; 
  6.             String oldName = valuesHolder.getPropertyName(); 
  7.             valuesHolder.setPropertyName(propertyName); 
  8.             mValuesMap.remove(oldName); 
  9.             mValuesMap.put(propertyName, valuesHolder); 
  10.         } 
  11.         mPropertyName = propertyName; 
  12.         // New property/values/target should cause re-initialization prior to starting 
  13.         mInitialized = false
  14.     } 
  • 记录下 propertyName 的名字;
  • 而如果已经有这个 propertyName,则会替换其相应的 PropertyValuesHolder,这里用了一个 HashMap 来保存 propertyName 和 PropertyValuesHolder
  • 如果propertyName 是 "translationX";
  • 接下来看 setFloatValues() 方法;

4、setFloatValues()

  1. @Override 
  2.     public void setFloatValues(float... values) { 
  3.         if (mValues == null || mValues.length == 0) { 
  4.             // 当前还没有任何值 
  5.             if (mProperty != null) { 
  6.                 setValues(PropertyValuesHolder.ofFloat(mProperty, values)); 
  7.             } else { 
  8.                 setValues(PropertyValuesHolder.ofFloat(mPropertyName, values)); 
  9.             } 
  10.         } else { 
  11.             // 当前已经有值的情况,调用父类的 setFloatValues() 
  12.             super.setFloatValues(values); 
  13.         } 
  14.     } 

父类,即 ValueAnimator ,其方法setFloatValues() 如下;

5、ValueAnimator#setFloatValues()

  1. public void setFloatValues(float... values) { 
  2.         if (values == null || values.length == 0) { 
  3.             return
  4.         } 
  5.         if (mValues == null || mValues.length == 0) { 
  6.             setValues(PropertyValuesHolder.ofFloat(""values)); 
  7.         } else { 
  8.             PropertyValuesHolder valuesHolder = mValues[0]; 
  9.             valuesHolder.setFloatValues(values); 
  10.         } 
  11.         // New property/values/target should cause re-initialization prior to starting 
  12.         mInitialized = false
  13.     } 
  • 不管是否调用父类的 setFloatValues();
  • 最后都是要将 values 逐个构造成 PropertyValuesHolder,最后存放在前面所说的 HashMap 里面;
  • 当然,如果这里的 hashMap 还没有初始化,则先会将其初始化;
  • 最关键的是要构建出 PropertyValuesHolder 这个对象;
  • 那么就继续来看看 PropertyValuesHolder#ofFloat() 方法;

6、PropertyValuesHolder#ofFloat()

  1.  public static PropertyValuesHolder ofFloat(String propertyName, float... values) { 
  2.         return new FloatPropertyValuesHolder(propertyName, values); 
  3.     } 
  4. 构造 FloatPropertyValuesHolder; 
  5. FloatPropertyValuesHolder 
  6.         public FloatPropertyValuesHolder(String propertyName, float... values) { 
  7.             super(propertyName); 
  8.             setFloatValues(values); 
  9.         } 
  • FloatPropertyValuesHolder 构造函数比较简单,调用父类的构造方法并传递了 propertyName;
  • 关键是进一步 setFloatValues() 方法的调用;
  • 其又进一步调用了父类的 setFloatValues(),在父类的 setFloatValues() 方法里初始化了动画的关键帧;
  1. PropertyValuesHolder#setFloatValues() 
  2.     public void setFloatValues(float... values) { 
  3.         mValueType = float.class; 
  4.         mKeyframes = KeyframeSet.ofFloat(values); 
  5.     } 
  • 进一步调用了 KeyframeSet#ofFloat() 方法以完成关键帧的构造;
  • KeyframeSet 是接口 Keyframe 的实现类;

7、KeyframeSet#ofFloat()

  1. public static KeyframeSet ofFloat(float... values) { 
  2.         boolean badValue = false
  3.         int numKeyframes = values.length; 
  4.         // 至少要 2 帧 
  5.         FloatKeyframe keyframes[] = new FloatKeyframe[Math.max(numKeyframes,2)]; 
  6.         // 然后构造出每一帧,每一帧中主要有 2 个重要的参数 fraction 以及 value 
  7.         if (numKeyframes == 1) { 
  8.             keyframes[0] = (FloatKeyframe) Keyframe.ofFloat(0f); 
  9.             keyframes[1] = (FloatKeyframe) Keyframe.ofFloat(1f, values[0]); 
  10.             if (Float.isNaN(values[0])) { 
  11.                 badValue = true
  12.             } 
  13.         } else { 
  14.             keyframes[0] = (FloatKeyframe) Keyframe.ofFloat(0f, values[0]); 
  15.             for (int i = 1; i < numKeyframes; ++i) { 
  16.                 keyframes[i] = 
  17.                         (FloatKeyframe) Keyframe.ofFloat((float) i / (numKeyframes - 1), values[i]); 
  18.                 if (Float.isNaN(values[i])) { 
  19.                     badValue = true
  20.                 } 
  21.             } 
  22.         } 
  23.         if (badValue) { 
  24.             Log.w("Animator""Bad value (NaN) in float animator"); 
  25.         } 
  26.         // 最后将所有的 关键帧 汇集到一个集合中 
  27.         return new FloatKeyframeSet(keyframes); 
  28.     } 

其主要内容是:

  • 构造动画的关键帧,且动画里至少要有 2 个关键帧;
  • 关键帧中有 2 个重要的参数,fraction这个可以看成是关键帧的序号,value 关键帧的值,可能是起始值,也可能是中间的某个值;
  • 最后将关键帧汇集成一个关键帧集返回给 PropertyValuesHolder;

8、setDuration()

  1. @Override 
  2.     @NonNull 
  3.     public ObjectAnimator setDuration(long duration) { 
  4.         super.setDuration(duration); 
  5.         return this; 
  6.     } 

调用了父类 ValueAnimator 的 setDuration();

  1. ValueAnimator#setDuration() 
  2.     @Override 
  3.     public ValueAnimator setDuration(long duration) { 
  4.         if (duration < 0) { 
  5.             throw new IllegalArgumentException("Animators cannot have negative duration: " + 
  6.                     duration); 
  7.         } 
  8.         mDuration = duration; 
  9.         return this; 
  10.     } 

setDuration() 只是简单的存储下 duration 的值,仅此而已,那么继续分析 setInterpolator();

9、setInterpolator()

  1. @Override 
  2.     public void setInterpolator(TimeInterpolator value) { 
  3.         if (value != null) { 
  4.             mInterpolator = value; 
  5.         } else { 
  6.             mInterpolator = new LinearInterpolator(); 
  7.         } 
  8.     } 

传递的是 null 的话,则默认使用的便是 LinearInterpolator,即线性插值器;

我们这里的假设的场景也是设置了 LinearInterpolator,这是最简单的插值器,其作用就是完成匀速运动;

10、 LinearInterpolator粗略分析;

  1. /** 
  2.  * 插值器定义了动画变化的频率,其可以是线性的也可以是非线性的,如加速运动或者减速运动; 
  3.  */ 
  4. public interface TimeInterpolator { 
  5.     /** 
  6.      * 这里传进来的 input 代表当前时间与总时间的比,根据这个时间占比返回当前的变化频率。其输出与输值都在 [0,1] 之间 
  7.      */ 
  8.     float getInterpolation(float input); 

插值器的关键定义便是实现 getInterpolation() 方法,即根据当前动画运行的时间占比来计算当前动画的变化频率;

那么来看看 LinearInterpolator 的 getInterpolation() 实现;

  1. LinearInterpolator#getInterpolation() 
  2. public float getInterpolation(float input) { 
  3.         return input; 
  4.     } 

对,就是返回原值,因为时间的变化肯定始终都是匀速的;

11、start

启动动画从 start() 方法开始

  1. @Override 
  2.     public void start() { 
  3.         AnimationHandler.getInstance().autoCancelBasedOn(this); 
  4.         if (DBG) { 
  5.             Log.d(LOG_TAG, "Anim target, duration: " + getTarget() + ", " + getDuration()); 
  6.             for (int i = 0; i < mValues.length; ++i) { 
  7.                 PropertyValuesHolder pvh = mValues[i]; 
  8.                 Log.d(LOG_TAG, "   Values[" + i + "]: " + 
  9.                     pvh.getPropertyName() + ", " + pvh.mKeyframes.getValue(0) + ", " + 
  10.                     pvh.mKeyframes.getValue(1)); 
  11.             } 
  12.         } 
  13.         super.start(); 
  14.     } 
  • 先确认动画已经取消;这个方法里的重要的那句代码就是调用父类 ValueAnimator 的 start();
  • 父类对外的 start() 方法很简单,其主要的实现在另一个重载的私有 start() 方法上;
  1. private void start(boolean playBackwards) { 
  2.        ..... 
  3.         mReversing = playBackwards; 
  4.         // 重置脉冲为 "true" 
  5.         mSelfPulse = !mSuppressSelfPulseRequested; 
  6.         ..... 
  7.         // 添加脉冲回调用 
  8.         addAnimationCallback(0); 
  9.         if (mStartDelay == 0 || mSeekFraction >= 0 || mReversing) { 
  10.             // If there's no start delay, init the animation and notify start listeners right away 
  11.             // to be consistent with the previous behavior. Otherwise, postpone this until the first 
  12.             // frame after the start delay. 
  13.             startAnimation(); 
  14.             if (mSeekFraction == -1) { 
  15.                 // No seek, start at play time 0. Note that the reason we are not using fraction 0 
  16.                 // is because for animations with 0 duration, we want to be consistent with pre-N 
  17.                 // behavior: skip to the final value immediately. 
  18.                 setCurrentPlayTime(0); 
  19.             } else { 
  20.                 setCurrentFraction(mSeekFraction); 
  21.             } 
  22.         } 
  23.     } 

其中之一是 addAnimationCallback(),其主要是向 AnimationHander 添加一个回调接口AnimationHandler.AnimationFrameCallback,如下代码;

  1. /** 
  2.      * Register to get a callback on the next frame after the delay. 
  3.      */ 
  4.     public void addAnimationFrameCallback(final AnimationFrameCallback callback, long delay) { 
  5.         if (mAnimationCallbacks.size() == 0) { 
  6.             getProvider().postFrameCallback(mFrameCallback); 
  7.         } 
  8.         if (!mAnimationCallbacks.contains(callback)) { 
  9.             mAnimationCallbacks.add(callback); 
  10.         } 
  11.         if (delay > 0) { 
  12.             mDelayedCallbackStartTime.put(callback, (SystemClock.uptimeMillis() + delay)); 
  13.         } 
  14.     } 
  • ValueAnimator 就实现了 AnimationFrameCallback,所以这里添加的是 ValueAnimator 的实例;
  • 最终被添加到 mAnimationCallbacks 这个队列中;

12、startAnimation()

  1. private void startAnimation() { 
  2.         ...... 
  3.         mAnimationEndRequested = false
  4.         initAnimation(); 
  5.         mRunning = true
  6.         if (mSeekFraction >= 0) { 
  7.             mOverallFraction = mSeekFraction; 
  8.         } else { 
  9.             mOverallFraction = 0f; 
  10.         } 
  11.         if (mListeners != null) { 
  12.             // 通过动画监听器动画开始了 
  13.             notifyStartListeners(); 
  14.         } 
  15.     } 

关键调用 initAnimation()

  1. void initAnimation() { 
  2.         if (!mInitialized) { 
  3.             int numValues = mValues.length; 
  4.             for (int i = 0; i < numValues; ++i) { 
  5.                 mValues[i].init(); 
  6.             } 
  7.             mInitialized = true
  8.         } 
  9.     } 

mValues 是 PropertyValuesHolder 数组,这里的目的是初始化 PropertyValuesHolder;

  1. void init() { 
  2.         if (mEvaluator == null) { 
  3.             // We already handle int and float automatically, but not their Object 
  4.             // equivalents 
  5.             mEvaluator = (mValueType == Integer.class) ? sIntEvaluator : 
  6.                     (mValueType == Float.class) ? sFloatEvaluator : 
  7.                     null
  8.         } 
  9.         if (mEvaluator != null) { 
  10.             // KeyframeSet knows how to evaluate the common types - only give it a custom 
  11.             // evaluator if one has been set on this class 
  12.             mKeyframes.setEvaluator(mEvaluator); 
  13.         } 
  14.     } 
  • init() 方法的主要目的是就是给关键帧设置估值器;
  • 前面调用的是 ObjectAnimator#ofFloat() 方法,所以这里默认给的就是 FloatEvaluator;
  • 接下来就会进一步调用 setCurrentPlayTime() 来开始动画;

13、setCurrentPlayTime()

  1. public void setCurrentPlayTime(long playTime) { 
  2.         float fraction = mDuration > 0 ? (float) playTime / mDuration : 1; 
  3.         setCurrentFraction(fraction); 
  4.     } 
  • 初始时调用的是setCurrentPlayTime(0),也就是 playTime 为 0,而 mDuration 就是我们自己通过 setDuration() 来设置的;
  • 所以这里得到的 fraction 也是 0;
  • 进一步看 setCurrentFraction() 方法;

14、setCurrentFraction

  1. public void setCurrentFraction(float fraction) { 
  2.         // 再次调用 initAnimation() ,前面初始化过了,所以这里是无用的 
  3.         initAnimation(); 
  4.         // 校准 fraction 为 [0, mRepeatCount + 1] 
  5.         fraction = clampFraction(fraction); 
  6.         mStartTimeCommitted = true; // do not allow start time to be compensated for jank 
  7.         if (isPulsingInternal()) { 
  8.             // 随机时间? 
  9.             long seekTime = (long) (getScaledDuration() * fraction); 
  10.             // 获取动画的当前运行时间 
  11.             long currentTime = AnimationUtils.currentAnimationTimeMillis(); 
  12.             // Only modify the start time when the animation is running. Seek fraction will ensure 
  13.             // non-running animations skip to the correct start time
  14.             // 得到开始时间 
  15.             mStartTime = currentTime - seekTime; 
  16.         } else { 
  17.             // If the animation loop hasn't started, or during start delay, the startTime will be 
  18.             // adjusted once the delay has passed based on seek fraction. 
  19.             mSeekFraction = fraction; 
  20.         } 
  21.         mOverallFraction = fraction; 
  22.         final float currentIterationFraction = getCurrentIterationFraction(fraction, mReversing); 
  23.         // 执行动画,注意这里会先调用子类的 animateValue() 方法 
  24.         animateValue(currentIterationFraction); 
  25.     } 

前面都是一些时间的计算,得到当前真正的currentIterationFraction,最后会通过调用animateValue() 来执行动画;

15、 ObjectAnimator#animateValue()

  1. void animateValue(float fraction) { 
  2.         final Object target = getTarget(); 
  3.         if (mTarget != null && target == null) { 
  4.             // We lost the target reference, cancel and clean up. Note: we allow null target if the 
  5.             /// target has never been set
  6.             cancel(); 
  7.             return
  8.         } 
  9.         // 调用父类的 animateValue() ,这个很关键,时间插值与估值器的计算都在父类的 animateValue() 方法中进行的。 
  10.         super.animateValue(fraction); 
  11.         int numValues = mValues.length; 
  12.         for (int i = 0; i < numValues; ++i) { 
  13.             // 这里的 mValues 的是PropertyValuesHolder[],也就是在 PropertyValuesHolder 里面来改变了目标 target 的属性值。 
  14.             mValues[i].setAnimatedValue(target); 
  15.         } 
  16.     } 

父类 ValueAnimator#animateValue()

  1. void animateValue(float fraction) { 
  2.         // 获取时间插值 
  3.         fraction = mInterpolator.getInterpolation(fraction); 
  4.         mCurrentFraction = fraction; 
  5.         int numValues = mValues.length; 
  6.         // 将时间插值送给估值器,计算出 values 
  7.         for (int i = 0; i < numValues; ++i) { 
  8.             mValues[i].calculateValue(fraction); 
  9.         } 
  10.        // 发出通知 
  11.         if (mUpdateListeners != null) { 
  12.             int numListeners = mUpdateListeners.size(); 
  13.             for (int i = 0; i < numListeners; ++i) { 
  14.                 mUpdateListeners.get(i).onAnimationUpdate(this); 
  15.             } 
  16.         } 
  17.     } 

animateValue(): 计算时间插值和估值器、调用 PropertyValuesHolder 来改变属性;

  1. void setAnimatedValue(Object target) { 
  2.         if (mProperty != null) { 
  3.             mProperty.set(target, getAnimatedValue()); 
  4.         } 
  5.         if (mSetter != null) { 
  6.             try { 
  7.                 mTmpValueArray[0] = getAnimatedValue(); 
  8.                 // 通过反射调用来修改属性值 
  9.                 mSetter.invoke(target, mTmpValueArray); 
  10.             } catch (InvocationTargetException e) { 
  11.                 Log.e("PropertyValuesHolder", e.toString()); 
  12.             } catch (IllegalAccessException e) { 
  13.                 Log.e("PropertyValuesHolder", e.toString()); 
  14.             } 
  15.         } 
  16.     } 
  • 这里就是通过属性的 Setter 方法来修改属性的;
  • 分析到这里,就完成了动画的一帧关键帧的执行;
  • 剩下的帧是怎么驱动的呢?还是得回到 start() 方法里面,在这里最初分析到 addAnimationFrameCallback() 方法;
  • 这个方法里等于是向AnimationHandler注册了AnimationHandler.AnimationFrameCallback;
  • 这个 callback 中其中之一的方法是 doAnimationFrame();

在 ValueAnimator 的实现中如下;

  1. public final boolean doAnimationFrame(long frameTime) { 
  2.         ..... 
  3.         boolean finished = animateBasedOnTime(currentTime); 
  4.         if (finished) { 
  5.             endAnimation(); 
  6.         } 
  7.         return finished; 
  8.     } 

这段代码原来也是很长的,我们只看关键调用 animateBasedOnTime()

  1. boolean animateBasedOnTime(long currentTime) { 
  2.         boolean done = false
  3.         if (mRunning) { 
  4.             ..... 
  5.             float currentIterationFraction = getCurrentIterationFraction( 
  6.                     mOverallFraction, mReversing); 
  7.             animateValue(currentIterationFraction); 
  8.         } 
  9.         return done; 
  10.     } 
  • 目的也还是计算出 currentIterationFraction;
  • 通过 animateValue() 方法来执行动画;
  • 可以看到只要 doAnimationFrame() 被不断的调用,就会产生动画的一个关键帧;
  • 如果关键帧是连续的,那么最后也就产生了我们所看到的动画;
  • 再来分析doAnimationFrame() 是如何被不断调用的;
  • 这个需要回到 AnimationHandler 中来,在 AnimationHandler 中有一个非常重要的 callback 实现——Choreographer.FrameCallback;
  1. private final Choreographer.FrameCallback mFrameCallback = new Choreographer.FrameCallback() { 
  2.         @Override 
  3.         public void doFrame(long frameTimeNanos) { 
  4.             doAnimationFrame(getProvider().getFrameTime()); 
  5.             if (mAnimationCallbacks.size() > 0) { 
  6.                 getProvider().postFrameCallback(this); 
  7.             } 
  8.         } 
  9.     }; 
  • Andorid 中的重绘就是由Choreographer在 1 秒内产生 60 个 vsync 来通知 view tree 进行 view 的重绘的;
  • 而 vsync 产生后会调用它的监听者回调接口 Choreographer.FrameCallback,;
  • 也就是说,只要向Choreographer注册了这个接口,就会每 1 秒里收到 60 次回调;
  • 因此,在这里就实现了不断地调用 doAnimationFrame() 来驱动动画了;

16、流程总结

  • 动画是由许多的关键帧组成的;
  • 属性动画的主要组成是 PropertyValuesHolder,而 PropertyValuesHolder 又封装了关键帧;
  • 动画开始后,其监听了 Choreographer 的 vsync,使得其可以不断地调用 doAnimationFrame() 来驱动动画执行每一个关键帧;
  • 每一次的 doAnimationFrame() 调用都会去计算时间插值,而通过时间插值器计算得到 fraction 又会传给估值器,使得估值器可以计算出属性的当前值;
  • 最后再通过 PropertyValuesHolder 所记录下的 Setter 方法,以反射的方式来修改目标属性的值;
  • 当属性值一帧一帧的改变后,形成连续后,便是我们所见到的动画;

总结

2021年最后一个月,大家一起加油努力;

 

 

责任编辑:武晓燕 来源: Android开发编程
相关推荐

2017-05-31 13:16:35

PHP运行机制原理解析

2023-02-28 09:07:18

ChatGPTAI

2019-12-06 10:59:20

JavaScript运行引擎

2021-07-12 09:45:36

NameServer 核心Conusmer

2021-01-12 14:46:34

Kubernetes开发存储

2021-07-05 07:51:43

JVM底层Python

2021-12-06 14:52:08

动画Android补间动画

2023-10-13 13:30:00

MySQL锁机制

2020-05-21 13:25:43

Spring组件架构

2023-08-11 07:44:40

TCP滑动窗口数据

2015-08-18 09:40:32

OpenStack Neutron虚拟网络

2023-11-16 09:01:37

Hadoop数据库

2015-08-19 10:36:24

Zigbee技术无线通信

2015-07-01 13:34:22

Kubernetes应用部署模型

2010-07-06 10:07:10

jQueryJSON

2021-06-16 15:18:03

鸿蒙HarmonyOS应用

2021-09-04 07:29:57

Android

2021-09-09 06:55:43

AndroidViewDragHel原理

2021-05-19 15:40:54

HTTPS前端加密

2009-11-04 13:08:29

ADO.NET Dat
点赞
收藏

51CTO技术栈公众号