framework 之 Animation (五)

2014-11-24 11:24:38 · 作者: · 浏览: 24
nal long duration = mDuration;
float normalizedTime;
if (duration != 0) {
normalizedTime = ((float) (currentTime - (mStartTime + startOffset))) /
(float) duration;
} else {
// time is a step-change with a zero duration
normalizedTime = currentTime < mStartTime 0.0f : 1.0f;
}

final boolean expired = normalizedTime >= 1.0f;
mMore = !expired;

if (!mFillEnabled) normalizedTime = Math.max(Math.min(normalizedTime, 1.0f), 0.0f);

// 前面的部分是计算normalizedTime,这里开始才是调用执行
if ((normalizedTime >= 0.0f || mFillBefore) && (normalizedTime <= 1.0f || mFillAfter)) {
if (!mStarted) {
if (mListener != null) {
mListener.onAnimationStart(this);
}
mStarted = true;
if (USE_CLOSEGUARD) {
guard.open("cancel or detach or getTransformation");
}
}

if (mFillEnabled) normalizedTime = Math.max(Math.min(normalizedTime, 1.0f), 0.0f);

if (mCycleFlip) {
normalizedTime = 1.0f - normalizedTime;
}

final float interpolatedTime = mInterpolator.getInterpolation(normalizedTime);
applyTransformation(interpolatedTime, outTransformation);
}
// 完成了applyTransformation的调用,后面计算动画是否循环

if (expired) {
if (mRepeatCount == mRepeated) {
if (!mEnded) {
mEnded = true;
guard.close();
if (mListener != null) {
mListener.onAnimationEnd(this);
}
}
} else {
if (mRepeatCount > 0) {
mRepeated++;
}

if (mRepeatMode == REVERSE) {
mCycleFlip = !mCycleFlip;
}

mStartTime = -1;
mMore = true;

if (mListener != null) {
mListener.onAnimationRepeat(this);
}
}
}

if (!mMore && mOneMoreTime) {
mOneMoreTime = false;
return true;
}

return mMore;
}这个方法中,还有一个成员是mListener,它的类型是AnimationListener,在这个Animation类的末尾,定义了AnimationListener接口,继承Animation类的子类可以使用此接口,来监听比如动画开始时要做保存工作,动画结束时要做清除及恢复工作等。

[java]
/**
*

An animation listener receives notifications from an animation.
* Notifications indicate animation related events, such as the end or the
* repetition of the animation.


*/
public static interface AnimationListener {
/**
*

Notifies the start of the animation.


*
* @param animation The started animation.
*/
void onAnimationStart(Animation animation);

/**
*

Notifies the end of the animation. This callback is not invoked
* for animations with repeat count set to INFINITE.


*
* @param animation The animation which reached its end.
*/
void onAnimationEnd(Animation animation);

/**
*

Notifies the repetition of the animation.


*
* @param animation The animation which was repeated.
*/
void onAnimationRepeat(Animation animation);
}

/**
*

An animation listener receives notifications from an animation.
* Notifications indicate animation related events, such as the end or the
* repetitio