Android开源框架:NineOldAndroid

时间:2022-09-28 22:59:31

在android3.0以前的版本,要实现动画,一般是使用NineOldAndroid开源框架,之后,就可以直接使用android提供的animation API了。

仔细看过此开源框架后,可看出此框架和系统的动画实现还有有区别的。主要但不仅仅一下几点:

1. 计算每帧动画的流程:AnimationHandler,NineOldAndroid中是继承于Handler,在消息队列中不断的处理每帧动画的数据,而系统则是实现了Runnable接口,在线程中进行处理

 //NineOldAndroid
private static class AnimationHandler extends Handler{
public void handlerMessage(){
....
sendEmptyMessageDelayed(...);
} //Android系统
protected static class AnimationHandler implements Runnable {
// The per-thread list of all active animations
/** @hide */
protected final ArrayList<ValueAnimator> mAnimations = new ArrayList<ValueAnimator>(); // Used in doAnimationFrame() to avoid concurrent modifications of mAnimations
private final ArrayList<ValueAnimator> mTmpAnimations = new ArrayList<ValueAnimator>(); // The per-thread set of animations to be started on the next animation frame
/** @hide */
protected final ArrayList<ValueAnimator> mPendingAnimations = new ArrayList<ValueAnimator>(); /**
* Internal per-thread collections used to avoid set collisions as animations start and end
* while being processed.
* @hide
*/
protected final ArrayList<ValueAnimator> mDelayedAnims = new ArrayList<ValueAnimator>();
private final ArrayList<ValueAnimator> mEndingAnims = new ArrayList<ValueAnimator>();
private final ArrayList<ValueAnimator> mReadyAnims = new ArrayList<ValueAnimator>(); private final Choreographer mChoreographer;
.....
}

2. Choreographer:不同于开源框架中,android系统还新引入了Choreographer(编舞者),通过此类来协调动画时间,及绘制等。最主要的是Choreographer中提供了VSync:垂直中断。

Choreographer会协调每帧时间,使用回调函数来执行Runnable.run。可使动画的显示更流畅,不会出现动画卡顿,画面撕裂等状况。关于VSync的一些更具体的知识可以百度。