为什么 Animation.cancel() 和 View.clearAnimation() 在我的情况下不起作用?
Why Animation.cancel() and View.clearAnimation() don't work in my case?
我不明白为什么这段代码不能按预期工作。我想取消动画。为了测试它,我打电话给开始动画的 setLoading(true);
并立即打电话给取消动画的 setLoading(false);
。
测试代码:
setLoading(true);
setLoading(false);
这里是代码:
private void setLoading(boolean loading) {
if (loading) {
Animation animation = AnimationUtils.loadAnimation(this, R.anim.fade_out);
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
Log.i(TAG, "start"); // for debug
}
@Override
public void onAnimationEnd(Animation animation) {
Log.i(TAG, "end"); // for debug
mButton.setVisibility(View.INVISIBLE);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
mButton.startAnimation(animation);
mLoading.setVisibility(View.VISIBLE);
} else {
Log.i(TAG, "cancel"); // for debug
mButton.getAnimation().cancel();
mButton.setVisibility(View.VISIBLE);
}
}
fade_out.xml:
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:fromAlpha="1.0"
android:startOffset="300"
android:toAlpha="0.0" />
Logcat:
start
cancel
end
预期结果:
start
cancel
为什么在 Animation.cancel()
或 View.clearAnimation()
之后调用 onAnimationEnd()
?
我尝试使用 Animation.cancel()
,使用 View.clearAnimation()
并同时使用两者。
提前致谢
它按预期工作,来自 Animatoin 源代码:
public void cancel() {
if (mStarted && !mEnded) {
fireAnimationEnd();
mEnded = true;
guard.close();
}
// Make sure we move the animation to the end
mStartTime = Long.MIN_VALUE;
mMore = mOneMoreTime = false;
}
private void fireAnimationEnd() {
if (mListener != null) {
if (mListenerHandler == null) mListener.onAnimationEnd(this);
else mListenerHandler.postAtFrontOfQueue(mOnEnd);
}
}
您可以在 onAnimationEnd
中检查它是否以这种方式取消
animation.getStartTime() == Long.MIN_VALUE
动画其实有这个方法:
private boolean isCanceled() {
return mStartTime == Long.MIN_VALUE;
}
但它是私人的。时间不同,因为在 cancel()
中你可以看到时间设置为 Long.MIN_VALUE
我不明白为什么这段代码不能按预期工作。我想取消动画。为了测试它,我打电话给开始动画的 setLoading(true);
并立即打电话给取消动画的 setLoading(false);
。
测试代码:
setLoading(true);
setLoading(false);
这里是代码:
private void setLoading(boolean loading) {
if (loading) {
Animation animation = AnimationUtils.loadAnimation(this, R.anim.fade_out);
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
Log.i(TAG, "start"); // for debug
}
@Override
public void onAnimationEnd(Animation animation) {
Log.i(TAG, "end"); // for debug
mButton.setVisibility(View.INVISIBLE);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
mButton.startAnimation(animation);
mLoading.setVisibility(View.VISIBLE);
} else {
Log.i(TAG, "cancel"); // for debug
mButton.getAnimation().cancel();
mButton.setVisibility(View.VISIBLE);
}
}
fade_out.xml:
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:fromAlpha="1.0"
android:startOffset="300"
android:toAlpha="0.0" />
Logcat:
start
cancel
end
预期结果:
start
cancel
为什么在 Animation.cancel()
或 View.clearAnimation()
之后调用 onAnimationEnd()
?
我尝试使用 Animation.cancel()
,使用 View.clearAnimation()
并同时使用两者。
提前致谢
它按预期工作,来自 Animatoin 源代码:
public void cancel() {
if (mStarted && !mEnded) {
fireAnimationEnd();
mEnded = true;
guard.close();
}
// Make sure we move the animation to the end
mStartTime = Long.MIN_VALUE;
mMore = mOneMoreTime = false;
}
private void fireAnimationEnd() {
if (mListener != null) {
if (mListenerHandler == null) mListener.onAnimationEnd(this);
else mListenerHandler.postAtFrontOfQueue(mOnEnd);
}
}
您可以在 onAnimationEnd
中检查它是否以这种方式取消animation.getStartTime() == Long.MIN_VALUE
动画其实有这个方法:
private boolean isCanceled() {
return mStartTime == Long.MIN_VALUE;
}
但它是私人的。时间不同,因为在 cancel()
中你可以看到时间设置为 Long.MIN_VALUE