Android源码解析之属性动画详解
前言
大家在日常开发中离不开动画,属性动画更为强大,我们不仅要知道如何使用,更要知道他的原理。这样,才能得心应手。那么,今天,就从最简单的来说,了解下属性动画的原理。
ObjectAnimator .ofInt(mView,"width",100,500) .setDuration(1000) .start();
ObjectAnimator#ofInt
以这个为例,代码如下。
publicstaticObjectAnimatorofInt(Objecttarget,StringpropertyName,int...values){
ObjectAnimatoranim=newObjectAnimator(target,propertyName);
anim.setIntValues(values);
returnanim;
}
在这个方法中,首先会new一个ObjectAnimator对象,然后通过setIntValues方法将值设置进去,然后返回。在ObjectAnimator的构造方法中,会通过setTarget方法设置当前动画的对象,通过setPropertyName设置当前的属性名。我们重点说下setIntValues方法。
publicvoidsetIntValues(int...values){
if(mValues==null||mValues.length==0){
//Novaluesyet-thisanimatorisbeingconstructedpiecemeal.Initthevalueswith
//whateverthecurrentpropertyNameis
if(mProperty!=null){
setValues(PropertyValuesHolder.ofInt(mProperty,values));
}else{
setValues(PropertyValuesHolder.ofInt(mPropertyName,values));
}
}else{
super.setIntValues(values);
}
}
首先会判断,mValues是不是null,我们这里是null,并且mProperty也是null,所以会调用
setValues(PropertyValuesHolder.ofInt(mPropertyName,values));方法。先看PropertyValuesHolder.ofInt方法,PropertyValuesHolder这个类是holds属性和值的,在这个方法会构造一个IntPropertyValuesHolder对象并返回。
publicstaticPropertyValuesHolderofInt(StringpropertyName,int...values){
returnnewIntPropertyValuesHolder(propertyName,values);
}
IntPropertyValuesHolder的构造方法如下:
publicIntPropertyValuesHolder(StringpropertyName,int...values){
super(propertyName);
setIntValues(values);
}
在这里,首先会调用他的分类的构造方法,然后调用setIntValues方法,在他父类的构造方法中,只是设置了下propertyName。setIntValues内容如下:
publicvoidsetIntValues(int...values){
super.setIntValues(values);
mIntKeyframes=(Keyframes.IntKeyframes)mKeyframes;
}
在父类的setIntValues方法中,初始化了mValueType为int.class,mKeyframes为KeyframeSet.ofInt(values)。其中KeyframeSet为关键帧集合。然后将mKeyframes赋值给mIntKeyframes。
KeyframeSet
这个类是记录关键帧的。我们看下他的ofInt方法。
publicstaticKeyframeSetofInt(int...values){
intnumKeyframes=values.length;
IntKeyframekeyframes[]=newIntKeyframe[Math.max(numKeyframes,2)];
if(numKeyframes==1){
keyframes[0]=(IntKeyframe)Keyframe.ofInt(0f);
keyframes[1]=(IntKeyframe)Keyframe.ofInt(1f,values[0]);
}else{
keyframes[0]=(IntKeyframe)Keyframe.ofInt(0f,values[0]);
for(inti=1;i<numKeyframes;++i){
keyframes[i]=
(IntKeyframe)Keyframe.ofInt((float)i/(numKeyframes-1),values[i]);
}
}
returnnewIntKeyframeSet(keyframes);
}
在这里呢?根据传入的values来计算关键帧,最后返回IntKeyframeSet。
回到ObjectAnimator里面,这里的setValues用的是父类ValueAnimator的
ValueAnimator#setValues
publicvoidsetValues(PropertyValuesHolder...values){
intnumValues=values.length;
mValues=values;
mValuesMap=newHashMap<String,PropertyValuesHolder>(numValues);
for(inti=0;i<numValues;++i){
PropertyValuesHoldervaluesHolder=values[i];
mValuesMap.put(valuesHolder.getPropertyName(),valuesHolder);
}
//Newproperty/values/targetshouldcausere-initializationpriortostarting
mInitialized=false;
}
这里的操作就简单了,就是把PropertyValuesHolder放入到mValuesMap中。
ObjectAnimator#start
这个方法就是动画开始的地方。
publicvoidstart(){
//Seeifanyofthecurrentactive/pendinganimatorsneedtobecanceled
AnimationHandlerhandler=sAnimationHandler.get();
if(handler!=null){
intnumAnims=handler.mAnimations.size();
for(inti=numAnims-1;i>=0;i--){
if(handler.mAnimations.get(i)instanceofObjectAnimator){
ObjectAnimatoranim=(ObjectAnimator)handler.mAnimations.get(i);
if(anim.mAutoCancel&&hasSameTargetAndProperties(anim)){
anim.cancel();
}
}
}
numAnims=handler.mPendingAnimations.size();
for(inti=numAnims-1;i>=0;i--){
if(handler.mPendingAnimations.get(i)instanceofObjectAnimator){
ObjectAnimatoranim=(ObjectAnimator)handler.mPendingAnimations.get(i);
if(anim.mAutoCancel&&hasSameTargetAndProperties(anim)){
anim.cancel();
}
}
}
numAnims=handler.mDelayedAnims.size();
for(inti=numAnims-1;i>=0;i--){
if(handler.mDelayedAnims.get(i)instanceofObjectAnimator){
ObjectAnimatoranim=(ObjectAnimator)handler.mDelayedAnims.get(i);
if(anim.mAutoCancel&&hasSameTargetAndProperties(anim)){
anim.cancel();
}
}
}
}
if(DBG){
Log.d(LOG_TAG,"Animtarget,duration:"+getTarget()+","+getDuration());
for(inti=0;i<mValues.length;++i){
PropertyValuesHolderpvh=mValues[i];
Log.d(LOG_TAG,"Values["+i+"]:"+
pvh.getPropertyName()+","+pvh.mKeyframes.getValue(0)+","+
pvh.mKeyframes.getValue(1));
}
}
super.start();
}
首先呢,会获取AnimationHandler对象,如果不为空的话,就会判断是mAnimations、mPendingAnimations、mDelayedAnims中的动画,并且取消。最后调用父类的start方法。
ValueAnimator#start
privatevoidstart(booleanplayBackwards){
if(Looper.myLooper()==null){
thrownewAndroidRuntimeException("AnimatorsmayonlyberunonLooperthreads");
}
mReversing=playBackwards;
mPlayingBackwards=playBackwards;
if(playBackwards&&mSeekFraction!=-1){
if(mSeekFraction==0&&mCurrentIteration==0){
//specialcase:reversingfromseek-to-0shouldactasifnotseekedatall
mSeekFraction=0;
}elseif(mRepeatCount==INFINITE){
mSeekFraction=1-(mSeekFraction%1);
}else{
mSeekFraction=1+mRepeatCount-(mCurrentIteration+mSeekFraction);
}
mCurrentIteration=(int)mSeekFraction;
mSeekFraction=mSeekFraction%1;
}
if(mCurrentIteration>0&&mRepeatMode==REVERSE&&
(mCurrentIteration<(mRepeatCount+1)||mRepeatCount==INFINITE)){
//ifwewereseekedtosomeotheriterationinareversinganimator,
//figureoutthecorrectdirectiontostartplayingbasedontheiteration
if(playBackwards){
mPlayingBackwards=(mCurrentIteration%2)==0;
}else{
mPlayingBackwards=(mCurrentIteration%2)!=0;
}
}
intprevPlayingState=mPlayingState;
mPlayingState=STOPPED;
mStarted=true;
mStartedDelay=false;
mPaused=false;
updateScaledDuration();//incasethescalefactorhaschangedsincecreationtime
AnimationHandleranimationHandler=getOrCreateAnimationHandler();
animationHandler.mPendingAnimations.add(this);
if(mStartDelay==0){
//Thissetstheinitialvalueoftheanimation,priortoactuallystartingitrunning
if(prevPlayingState!=SEEKED){
setCurrentPlayTime(0);
}
mPlayingState=STOPPED;
mRunning=true;
notifyStartListeners();
}
animationHandler.start();
}
- 先初始化一些值
- updateScaledDuration缩放时间,默认为1.0f
- 获取或者创建AnimationHandler,将动画加入到mPendingAnimations列表中,
- 如果没延迟,通知监听器
- animationHandler.start
在animationHandler.start中,会调用scheduleAnimation方法,在这个种,会用mChoreographerpost一个callback,最终会执行mAnimate的run方法。mChoreographerpost涉及到VSYNC,这里不多介绍。
mAnimate#run
doAnimationFrame(mChoreographer.getFrameTime());
在这里会用过doAnimationFrame设置动画帧,我们看下这个方法的代码。
voiddoAnimationFrame(longframeTime){
mLastFrameTime=frameTime;
//mPendingAnimationsholdsanyanimationsthathaverequestedtobestarted
//We'regoingtoclearmPendingAnimations,butstartinganimationmay
//causemoretobeaddedtothependinglist(forexample,ifoneanimation
//startingtriggersanotherstarting).SoweloopuntilmPendingAnimations
//isempty.
while(mPendingAnimations.size()>0){
ArrayList<ValueAnimator>pendingCopy=
(ArrayList<ValueAnimator>)mPendingAnimations.clone();
mPendingAnimations.clear();
intcount=pendingCopy.size();
for(inti=0;i<count;++i){
ValueAnimatoranim=pendingCopy.get(i);
//IftheanimationhasastartDelay,placeitonthedelayedlist
if(anim.mStartDelay==0){
anim.startAnimation(this);
}else{
mDelayedAnims.add(anim);
}
}
}
//Next,processanimationscurrentlysittingonthedelayedqueue,adding
//themtotheactiveanimationsiftheyareready
intnumDelayedAnims=mDelayedAnims.size();
for(inti=0;i<numDelayedAnims;++i){
ValueAnimatoranim=mDelayedAnims.get(i);
if(anim.delayedAnimationFrame(frameTime)){
mReadyAnims.add(anim);
}
}
intnumReadyAnims=mReadyAnims.size();
if(numReadyAnims>0){
for(inti=0;i<numReadyAnims;++i){
ValueAnimatoranim=mReadyAnims.get(i);
anim.startAnimation(this);
anim.mRunning=true;
mDelayedAnims.remove(anim);
}
mReadyAnims.clear();
}
//Nowprocessallactiveanimations.ThereturnvaluefromanimationFrame()
//tellsthehandlerwhetheritshouldnowbeended
intnumAnims=mAnimations.size();
for(inti=0;i<numAnims;++i){
mTmpAnimations.add(mAnimations.get(i));
}
for(inti=0;i<numAnims;++i){
ValueAnimatoranim=mTmpAnimations.get(i);
if(mAnimations.contains(anim)&&anim.doAnimationFrame(frameTime)){
mEndingAnims.add(anim);
}
}
mTmpAnimations.clear();
if(mEndingAnims.size()>0){
for(inti=0;i<mEndingAnims.size();++i){
mEndingAnims.get(i).endAnimation(this);
}
mEndingAnims.clear();
}
//Schedulefinalcommitfortheframe.
mChoreographer.postCallback(Choreographer.CALLBACK_COMMIT,mCommit,null);
//Iftherearestillactiveordelayedanimations,scheduleafuturecallto
//onAnimatetoprocessthenextframeoftheanimations.
if(!mAnimations.isEmpty()||!mDelayedAnims.isEmpty()){
scheduleAnimation();
}
}
方法较长,逻辑如下:
- 从mPendingAnimations中取出动画,根据事先选择startAnimation还是加入到mDelayedAnims列表。
- 如果mDelayedAnims列表中的动画准备好了,就加入到mReadyAnims列表中
- 从mAnimations列表中取出要执行的动画,加入到mTmpAnimations列表
- 通过doAnimationFrame方法执行动画帧
- 继续执行scheduleAnimation
从上面我们能看出,执行动画的关键是doAnimationFrame方法。在这个方法中,会调用animationFrame方法。
ValueAniator#animationFrame
booleananimationFrame(longcurrentTime){
booleandone=false;
switch(mPlayingState){
caseRUNNING:
caseSEEKED:
floatfraction=mDuration>0?(float)(currentTime-mStartTime)/mDuration:1f;
if(mDuration==0&&mRepeatCount!=INFINITE){
//Skiptotheend
mCurrentIteration=mRepeatCount;
if(!mReversing){
mPlayingBackwards=false;
}
}
if(fraction>=1f){
if(mCurrentIteration<mRepeatCount||mRepeatCount==INFINITE){
//Timetorepeat
if(mListeners!=null){
intnumListeners=mListeners.size();
for(inti=0;i<numListeners;++i){
mListeners.get(i).onAnimationRepeat(this);
}
}
if(mRepeatMode==REVERSE){
mPlayingBackwards=!mPlayingBackwards;
}
mCurrentIteration+=(int)fraction;
fraction=fraction%1f;
mStartTime+=mDuration;
//Note:WedonotneedtoupdatethevalueofmStartTimeCommittedhere
//sincewejustaddedadurationoffset.
}else{
done=true;
fraction=Math.min(fraction,1.0f);
}
}
if(mPlayingBackwards){
fraction=1f-fraction;
}
animateValue(fraction);
break;
}
returndone;
}
- 计算fraction
- 调用animateValue方法
根据虚拟机执行引擎动态分派原则,这里会调用ObjectAnimator的animateValue方法。
ObjectAnimator#animateValue
voidanimateValue(floatfraction){
finalObjecttarget=getTarget();
if(mTarget!=null&&target==null){
//Welostthetargetreference,cancelandcleanup.
cancel();
return;
}
super.animateValue(fraction);
intnumValues=mValues.length;
for(inti=0;i<numValues;++i){
mValues[i].setAnimatedValue(target);
}
}
这里主要干了两件事,
- 调用父类的animateValue方法
- 通过setAnimatedValue设置属性
其父类的方法如下:
voidanimateValue(floatfraction){
fraction=mInterpolator.getInterpolation(fraction);
mCurrentFraction=fraction;
intnumValues=mValues.length;
for(inti=0;i<numValues;++i){
mValues[i].calculateValue(fraction);
}
if(mUpdateListeners!=null){
intnumListeners=mUpdateListeners.size();
for(inti=0;i<numListeners;++i){
mUpdateListeners.get(i).onAnimationUpdate(this);
}
}
}
在这个方法中,会通过Interpolator得到出当前的fraction,并通过calculateValue来计算当前应该的值,这里会调用IntPropertyValuesHolder的calculateValue
voidcalculateValue(floatfraction){
mIntAnimatedValue=mIntKeyframes.getIntValue(fraction);
}
我们知道,mIntKeyframes对应的是IntKeyframeSet。在这个类的getIntValue中,会通过TypeEvaluator来计算当前对应的值。不多说了。
最后,回到animateValue。计算了值之后,会调用setAnimatedValue来设置值。我们看看他的实现。
IntPropertyValuesHolder#setAnimatedValue
voidsetAnimatedValue(Objecttarget){
if(mIntProperty!=null){
mIntProperty.setValue(target,mIntAnimatedValue);
return;
}
if(mProperty!=null){
mProperty.set(target,mIntAnimatedValue);
return;
}
if(mJniSetter!=0){
nCallIntMethod(target,mJniSetter,mIntAnimatedValue);
return;
}
if(mSetter!=null){
try{
mTmpValueArray[0]=mIntAnimatedValue;
mSetter.invoke(target,mTmpValueArray);
}catch(InvocationTargetExceptione){
Log.e("PropertyValuesHolder",e.toString());
}catch(IllegalAccessExceptione){
Log.e("PropertyValuesHolder",e.toString());
}
}
}
恩,到这里就能看到修改属性值得痕迹了,有以下四种情况
- mIntProperty不为null
- mProperty不为null
- mJniSetter不为null
- mSetter不为null
首先,我们通过StringpropertyName,int…values参数构造的对象,mIntProperty为null,并且mProperty也为null。那其他两个是怎么来的呢?似乎漏了什么?
还节的,在doAnimationFrame中,直接调用startAnimation么?没错,就是这里。
startAnimation
在这个方法中调用了initAnimation方法。还是根据动态分派规则,这里调用ObjectAnimator的initAnimation方法。在这里调用PropertyValuesHolder的setupSetterAndGetter方法,在这里对mSetter等进行了初始化,这里就不多说了,大家自己看代码吧。
好了,以上就是关于Android中属性动画对的全部内容,希望本文的内容对各位Android开发者们能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对毛票票的支持。