Android源碼解析之屬性動(dòng)畫詳解
前言
大家在日常開發(fā)中離不開動(dòng)畫,屬性動(dòng)畫更為強(qiáng)大,我們不僅要知道如何使用,更要知道他的原理。這樣,才能得心應(yīng)手。那么,今天,就從最簡(jiǎn)單的來說,了解下屬性動(dòng)畫的原理。
ObjectAnimator .ofInt(mView,"width",100,500) .setDuration(1000) .start();
ObjectAnimator#ofInt
以這個(gè)為例,代碼如下。
public static ObjectAnimator ofInt(Object target, String propertyName, int... values) {
ObjectAnimator anim = new ObjectAnimator(target, propertyName);
anim.setIntValues(values);
return anim;
}
在這個(gè)方法中,首先會(huì)new一個(gè)ObjectAnimator對(duì)象,然后通過setIntValues方法將值設(shè)置進(jìn)去,然后返回。在ObjectAnimator的構(gòu)造方法中,會(huì)通過setTarget方法設(shè)置當(dāng)前動(dòng)畫的對(duì)象,通過setPropertyName設(shè)置當(dāng)前的屬性名。我們重點(diǎn)說下setIntValues方法。
public void setIntValues(int... values) {
if (mValues == null || mValues.length == 0) {
// No values yet - this animator is being constructed piecemeal. Init the values with
// whatever the current propertyName is
if (mProperty != null) {
setValues(PropertyValuesHolder.ofInt(mProperty, values));
} else {
setValues(PropertyValuesHolder.ofInt(mPropertyName, values));
}
} else {
super.setIntValues(values);
}
}
首先會(huì)判斷,mValues是不是null,我們這里是null,并且mProperty也是null,所以會(huì)調(diào)用
setValues(PropertyValuesHolder.ofInt(mPropertyName, values));方法。先看PropertyValuesHolder.ofInt方法,PropertyValuesHolder這個(gè)類是holds屬性和值的,在這個(gè)方法會(huì)構(gòu)造一個(gè)IntPropertyValuesHolder對(duì)象并返回。
public static PropertyValuesHolder ofInt(String propertyName, int... values) {
return new IntPropertyValuesHolder(propertyName, values);
}
IntPropertyValuesHolder的構(gòu)造方法如下:
public IntPropertyValuesHolder(String propertyName, int... values) {
super(propertyName);
setIntValues(values);
}
在這里,首先會(huì)調(diào)用他的分類的構(gòu)造方法,然后調(diào)用setIntValues方法,在他父類的構(gòu)造方法中,只是設(shè)置了下propertyName。setIntValues內(nèi)容如下:
public void setIntValues(int... values) {
super.setIntValues(values);
mIntKeyframes = (Keyframes.IntKeyframes) mKeyframes;
}
在父類的setIntValues方法中,初始化了mValueType為int.class,mKeyframes為KeyframeSet.ofInt(values)。其中KeyframeSet為關(guān)鍵幀集合。然后將mKeyframes賦值給mIntKeyframes。
KeyframeSet
這個(gè)類是記錄關(guān)鍵幀的。我們看下他的ofInt方法。
public static KeyframeSet ofInt(int... values) {
int numKeyframes = values.length;
IntKeyframe keyframes[] = new IntKeyframe[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 (int i = 1; i < numKeyframes; ++i) {
keyframes[i] =
(IntKeyframe) Keyframe.ofInt((float) i / (numKeyframes - 1), values[i]);
}
}
return new IntKeyframeSet(keyframes);
}
在這里呢?根據(jù)傳入的values來計(jì)算關(guān)鍵幀,最后返回IntKeyframeSet。
回到ObjectAnimator里面,這里的setValues用的是父類ValueAnimator的
ValueAnimator#setValues
public void setValues(PropertyValuesHolder... values) {
int numValues = values.length;
mValues = values;
mValuesMap = new HashMap<String, PropertyValuesHolder>(numValues);
for (int i = 0; i < numValues; ++i) {
PropertyValuesHolder valuesHolder = values[i];
mValuesMap.put(valuesHolder.getPropertyName(), valuesHolder);
}
// New property/values/target should cause re-initialization prior to starting
mInitialized = false;
}
這里的操作就簡(jiǎn)單了,就是把PropertyValuesHolder放入到mValuesMap中。
ObjectAnimator#start
這個(gè)方法就是動(dòng)畫開始的地方。
public void start() {
// See if any of the current active/pending animators need to be canceled
AnimationHandler handler = sAnimationHandler.get();
if (handler != null) {
int numAnims = handler.mAnimations.size();
for (int i = numAnims - 1; i >= 0; i--) {
if (handler.mAnimations.get(i) instanceof ObjectAnimator) {
ObjectAnimator anim = (ObjectAnimator) handler.mAnimations.get(i);
if (anim.mAutoCancel && hasSameTargetAndProperties(anim)) {
anim.cancel();
}
}
}
numAnims = handler.mPendingAnimations.size();
for (int i = numAnims - 1; i >= 0; i--) {
if (handler.mPendingAnimations.get(i) instanceof ObjectAnimator) {
ObjectAnimator anim = (ObjectAnimator) handler.mPendingAnimations.get(i);
if (anim.mAutoCancel && hasSameTargetAndProperties(anim)) {
anim.cancel();
}
}
}
numAnims = handler.mDelayedAnims.size();
for (int i = numAnims - 1; i >= 0; i--) {
if (handler.mDelayedAnims.get(i) instanceof ObjectAnimator) {
ObjectAnimator anim = (ObjectAnimator) handler.mDelayedAnims.get(i);
if (anim.mAutoCancel && hasSameTargetAndProperties(anim)) {
anim.cancel();
}
}
}
}
if (DBG) {
Log.d(LOG_TAG, "Anim target, duration: " + getTarget() + ", " + getDuration());
for (int i = 0; i < mValues.length; ++i) {
PropertyValuesHolder pvh = mValues[i];
Log.d(LOG_TAG, " Values[" + i + "]: " +
pvh.getPropertyName() + ", " + pvh.mKeyframes.getValue(0) + ", " +
pvh.mKeyframes.getValue(1));
}
}
super.start();
}
首先呢,會(huì)獲取AnimationHandler對(duì)象,如果不為空的話,就會(huì)判斷是mAnimations、mPendingAnimations、mDelayedAnims中的動(dòng)畫,并且取消。最后調(diào)用父類的start方法。
ValueAnimator#start
private void start(boolean playBackwards) {
if (Looper.myLooper() == null) {
throw new AndroidRuntimeException("Animators may only be run on Looper threads");
}
mReversing = playBackwards;
mPlayingBackwards = playBackwards;
if (playBackwards && mSeekFraction != -1) {
if (mSeekFraction == 0 && mCurrentIteration == 0) {
// special case: reversing from seek-to-0 should act as if not seeked at all
mSeekFraction = 0;
} else if (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)) {
// if we were seeked to some other iteration in a reversing animator,
// figure out the correct direction to start playing based on the iteration
if (playBackwards) {
mPlayingBackwards = (mCurrentIteration % 2) == 0;
} else {
mPlayingBackwards = (mCurrentIteration % 2) != 0;
}
}
int prevPlayingState = mPlayingState;
mPlayingState = STOPPED;
mStarted = true;
mStartedDelay = false;
mPaused = false;
updateScaledDuration(); // in case the scale factor has changed since creation time
AnimationHandler animationHandler = getOrCreateAnimationHandler();
animationHandler.mPendingAnimations.add(this);
if (mStartDelay == 0) {
// This sets the initial value of the animation, prior to actually starting it running
if (prevPlayingState != SEEKED) {
setCurrentPlayTime(0);
}
mPlayingState = STOPPED;
mRunning = true;
notifyStartListeners();
}
animationHandler.start();
}
- 先初始化一些值
- updateScaledDuration 縮放時(shí)間,默認(rèn)為1.0f
- 獲取或者創(chuàng)建AnimationHandler,將動(dòng)畫加入到mPendingAnimations列表中,
- 如果沒延遲,通知監(jiān)聽器
- animationHandler.start
在animationHandler.start中,會(huì)調(diào)用scheduleAnimation方法,在這個(gè)種,會(huì)用mChoreographerpost一個(gè)callback,最終會(huì)執(zhí)行mAnimate的run方法。mChoreographerpost涉及到VSYNC,這里不多介紹。
mAnimate#run
doAnimationFrame(mChoreographer.getFrameTime());
在這里會(huì)用過doAnimationFrame設(shè)置動(dòng)畫幀,我們看下這個(gè)方法的代碼。
void doAnimationFrame(long frameTime) {
mLastFrameTime = frameTime;
// mPendingAnimations holds any animations that have requested to be started
// We're going to clear mPendingAnimations, but starting animation may
// cause more to be added to the pending list (for example, if one animation
// starting triggers another starting). So we loop until mPendingAnimations
// is empty.
while (mPendingAnimations.size() > 0) {
ArrayList<ValueAnimator> pendingCopy =
(ArrayList<ValueAnimator>) mPendingAnimations.clone();
mPendingAnimations.clear();
int count = pendingCopy.size();
for (int i = 0; i < count; ++i) {
ValueAnimator anim = pendingCopy.get(i);
// If the animation has a startDelay, place it on the delayed list
if (anim.mStartDelay == 0) {
anim.startAnimation(this);
} else {
mDelayedAnims.add(anim);
}
}
}
// Next, process animations currently sitting on the delayed queue, adding
// them to the active animations if they are ready
int numDelayedAnims = mDelayedAnims.size();
for (int i = 0; i < numDelayedAnims; ++i) {
ValueAnimator anim = mDelayedAnims.get(i);
if (anim.delayedAnimationFrame(frameTime)) {
mReadyAnims.add(anim);
}
}
int numReadyAnims = mReadyAnims.size();
if (numReadyAnims > 0) {
for (int i = 0; i < numReadyAnims; ++i) {
ValueAnimator anim = mReadyAnims.get(i);
anim.startAnimation(this);
anim.mRunning = true;
mDelayedAnims.remove(anim);
}
mReadyAnims.clear();
}
// Now process all active animations. The return value from animationFrame()
// tells the handler whether it should now be ended
int numAnims = mAnimations.size();
for (int i = 0; i < numAnims; ++i) {
mTmpAnimations.add(mAnimations.get(i));
}
for (int i = 0; i < numAnims; ++i) {
ValueAnimator anim = mTmpAnimations.get(i);
if (mAnimations.contains(anim) && anim.doAnimationFrame(frameTime)) {
mEndingAnims.add(anim);
}
}
mTmpAnimations.clear();
if (mEndingAnims.size() > 0) {
for (int i = 0; i < mEndingAnims.size(); ++i) {
mEndingAnims.get(i).endAnimation(this);
}
mEndingAnims.clear();
}
// Schedule final commit for the frame.
mChoreographer.postCallback(Choreographer.CALLBACK_COMMIT, mCommit, null);
// If there are still active or delayed animations, schedule a future call to
// onAnimate to process the next frame of the animations.
if (!mAnimations.isEmpty() || !mDelayedAnims.isEmpty()) {
scheduleAnimation();
}
}
方法較長(zhǎng),邏輯如下:
- 從mPendingAnimations中取出動(dòng)畫,根據(jù)事先選擇startAnimation還是加入到mDelayedAnims列表。
- 如果mDelayedAnims列表中的動(dòng)畫準(zhǔn)備好了,就加入到mReadyAnims列表中
- 從mAnimations列表中取出要執(zhí)行的動(dòng)畫,加入到mTmpAnimations列表
- 通過doAnimationFrame方法執(zhí)行動(dòng)畫幀
- 繼續(xù)執(zhí)行scheduleAnimation
從上面我們能看出,執(zhí)行動(dòng)畫的關(guān)鍵是doAnimationFrame方法。在這個(gè)方法中,會(huì)調(diào)用animationFrame方法。
ValueAniator#animationFrame
boolean animationFrame(long currentTime) {
boolean done = false;
switch (mPlayingState) {
case RUNNING:
case SEEKED:
float fraction = mDuration > 0 ? (float)(currentTime - mStartTime) / mDuration : 1f;
if (mDuration == 0 && mRepeatCount != INFINITE) {
// Skip to the end
mCurrentIteration = mRepeatCount;
if (!mReversing) {
mPlayingBackwards = false;
}
}
if (fraction >= 1f) {
if (mCurrentIteration < mRepeatCount || mRepeatCount == INFINITE) {
// Time to repeat
if (mListeners != null) {
int numListeners = mListeners.size();
for (int i = 0; i < numListeners; ++i) {
mListeners.get(i).onAnimationRepeat(this);
}
}
if (mRepeatMode == REVERSE) {
mPlayingBackwards = !mPlayingBackwards;
}
mCurrentIteration += (int) fraction;
fraction = fraction % 1f;
mStartTime += mDuration;
// Note: We do not need to update the value of mStartTimeCommitted here
// since we just added a duration offset.
} else {
done = true;
fraction = Math.min(fraction, 1.0f);
}
}
if (mPlayingBackwards) {
fraction = 1f - fraction;
}
animateValue(fraction);
break;
}
return done;
}
- 計(jì)算fraction
- 調(diào)用animateValue方法
根據(jù)虛擬機(jī)執(zhí)行引擎動(dòng)態(tài)分派原則,這里會(huì)調(diào)用ObjectAnimator的animateValue方法。
ObjectAnimator#animateValue
void animateValue(float fraction) {
final Object target = getTarget();
if (mTarget != null && target == null) {
// We lost the target reference, cancel and clean up.
cancel();
return;
}
super.animateValue(fraction);
int numValues = mValues.length;
for (int i = 0; i < numValues; ++i) {
mValues[i].setAnimatedValue(target);
}
}
這里主要干了兩件事,
- 調(diào)用父類的animateValue方法
- 通過setAnimatedValue設(shè)置屬性
其父類的方法如下:
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);
}
}
}
在這個(gè)方法中,會(huì)通過Interpolator得到出當(dāng)前的fraction,并通過calculateValue來計(jì)算當(dāng)前應(yīng)該的值,這里會(huì)調(diào)用IntPropertyValuesHolder的calculateValue
void calculateValue(float fraction) {
mIntAnimatedValue = mIntKeyframes.getIntValue(fraction);
}
我們知道,mIntKeyframes對(duì)應(yīng)的是IntKeyframeSet。在這個(gè)類的getIntValue中,會(huì)通過TypeEvaluator來計(jì)算當(dāng)前對(duì)應(yīng)的值。不多說了。
最后,回到animateValue。計(jì)算了值之后,會(huì)調(diào)用setAnimatedValue來設(shè)置值。我們看看他的實(shí)現(xiàn)。
IntPropertyValuesHolder#setAnimatedValue
void setAnimatedValue(Object target) {
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 (InvocationTargetException e) {
Log.e("PropertyValuesHolder", e.toString());
} catch (IllegalAccessException e) {
Log.e("PropertyValuesHolder", e.toString());
}
}
}
恩,到這里就能看到修改屬性值得痕跡了,有以下四種情況
- mIntProperty不為null
- mProperty不為null
- mJniSetter不為null
- mSetter不為null
首先,我們通過String propertyName, int… values參數(shù)構(gòu)造的對(duì)象,mIntProperty為null,并且mProperty也為null。那其他兩個(gè)是怎么來的呢?似乎漏了什么?
還節(jié)的,在doAnimationFrame中,直接調(diào)用startAnimation么?沒錯(cuò),就是這里。
startAnimation
在這個(gè)方法中調(diào)用了initAnimation方法。還是根據(jù)動(dòng)態(tài)分派規(guī)則,這里調(diào)用ObjectAnimator的initAnimation方法。在這里調(diào)用PropertyValuesHolder的setupSetterAndGetter方法,在這里對(duì)mSetter等進(jìn)行了初始化,這里就不多說了,大家自己看代碼吧。
好了,以上就是關(guān)于Android中屬性動(dòng)畫對(duì)的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)各位Android開發(fā)者們能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- Android動(dòng)畫系列之屬性動(dòng)畫的基本使用教程
- Android屬性動(dòng)畫實(shí)現(xiàn)圖片從左到右逐漸消失
- Android動(dòng)畫教程之屬性動(dòng)畫詳解
- Android利用屬性動(dòng)畫實(shí)現(xiàn)優(yōu)酷菜單
- Android屬性動(dòng)畫特點(diǎn)詳解
- Android使用屬性動(dòng)畫如何自定義倒計(jì)時(shí)控件詳解
- Android屬性動(dòng)畫之ValueAnimator代碼詳解
- Android 屬性動(dòng)畫ValueAnimator與插值器詳解
- Android深入分析屬性動(dòng)畫源碼
相關(guān)文章
Android利用CountDownTimer實(shí)現(xiàn)點(diǎn)擊獲取驗(yàn)證碼倒計(jì)時(shí)效果
這篇文章主要為大家詳細(xì)介紹了Android利用CountDownTimer實(shí)現(xiàn)點(diǎn)擊獲取驗(yàn)證碼倒計(jì)時(shí)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
Android?autojs隨時(shí)翻譯剪貼板單詞實(shí)現(xiàn)示例
這篇文章主要為大家介紹了Android?autojs隨時(shí)翻譯剪貼板單詞,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Android項(xiàng)目實(shí)戰(zhàn)之ListView懸浮頭部展現(xiàn)效果實(shí)現(xiàn)
這篇文章主要給大家介紹了Android項(xiàng)目實(shí)戰(zhàn)之ListView懸浮頭部展現(xiàn)效果實(shí)現(xiàn)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-01-01
RecyclerView實(shí)現(xiàn)列表倒計(jì)時(shí)
這篇文章主要為大家詳細(xì)介紹了RecyclerView實(shí)現(xiàn)列表倒計(jì)時(shí),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09
Android開發(fā)Input系統(tǒng)觸摸事件分發(fā)
這篇文章主要為大家介紹了Android開發(fā)Input系統(tǒng)觸摸事件分發(fā)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
android 解析json數(shù)據(jù)格式的方法
這篇文章主要介紹了android 解析json數(shù)據(jù)格式的方法,有需要的朋友可以參考一下2014-01-01

