【发布时间】:2017-05-13 23:38:10
【问题描述】:
我在 ImageView 上应用了无限动画来指示我的应用程序中正在运行的后台线程。当线程完成时,我可以使用 clearAnimation() 停止动画,但它会将 ImageView 捕捉回其起始位置,并且我希望当前动画周期完成(旨在优雅地结束其起始位置)。有没有办法做到这一点?
【问题讨论】:
标签: android
我在 ImageView 上应用了无限动画来指示我的应用程序中正在运行的后台线程。当线程完成时,我可以使用 clearAnimation() 停止动画,但它会将 ImageView 捕捉回其起始位置,并且我希望当前动画周期完成(旨在优雅地结束其起始位置)。有没有办法做到这一点?
【问题讨论】:
标签: android
注册一个AnimationListener,然后等到onAnimationRepeat() 清除它。我还没有尝试过,但我认为它会起作用。
【讨论】:
onAnimationEnd() 上再次启动动画。不过,不确定它是否会像无限的多样性一样流畅。但是,如果这行得通,您就不会在您希望它消失的动画结尾处开始下一个动画。
只需拨打setRepeatCount(0),然后收听onAnimationEnd。详情请见here。
【讨论】:
您可以在动画侦听器的 onAnimationEnd() 方法中设置动画的理想位置。然后,视图将显示在您在那里设置的坐标处,而不是转到初始位置。
animation = new TranslateAnimation(0, -length, 0, 0); //setup here your translation parameters
animation.setInterpolator(new LinearInterpolator());
animation.setDuration(10);
animation.setAnimationListener(new Animation.AnimationListener() {
public void onAnimationStart(Animation animation) {
animationRunning = true;
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationEnd(Animation animation) {
animationRunning = false;
// setAnimationPositionAfterPositionChange();
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) runningText.getLayoutParams();
params.leftMargin = currentPosition; //Calculate you current position here
runningText.setLayoutParams(params);
}
});
runningText.setAnimation(animation);
【讨论】:
如果您使用的是 animate() 你使用 setListener(null) 来停止它的动画, 对我有用。
image.animate()
.translationXBy(-width* 0.5f)
.setDuration(300)
.setStartDelay(6000)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
image.animate().rotationBy(90).setDuration(1000).setStartDelay(1).setListener(null);
}
});
【讨论】: