我花了很长时间试图在继续代码之前找出如何等待动画完成。 到目前为止,我已经尝试了while(animation.hasEnded()== False),使用Thread.sleep,使用计时器,但似乎无法使任何工作。
出现了很多次的事情是为动画添加一个动画侦听器,我已经尝试了但不确定如何进一步推进。
我创建动画,启动动画然后有一行代码,我只想在动画结束后执行:
Animation moveDown = allAnimStuff(duration); //creates animation
image.startAnimation(moveDown); // Start the animation
displayScore(score); // wait for animation to finish before executing this line
以下是用于创建动画的allAnimStuff(持续时间)方法:
private Animation allAnimStuff(final long duration) {
Animation moveDown = new TranslateAnimation(image.getX(), image.getX(), (-image.getHeight()), pxHeight - image.getHeight() / 2); //creates animation
moveDown.setDuration(duration);
moveDown.setFillAfter(false);
moveDown.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
//some code to make it wait here?
}
@Override
public void onAnimationEnd(Animation animation) {
image.setY((pxHeight / 2 - image.getHeight()));
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
return moveDown;
}
答案 0 :(得分:4)
在onAnimationEnd方法中编写以下行
displayScore(score);
例如
private Animation allAnimStuff(final long duration) {
Animation moveDown = new TranslateAnimation(image.getX(), image.getX(), (-image.getHeight()), pxHeight - image.getHeight() / 2); //creates animation
moveDown.setDuration(duration);
moveDown.setFillAfter(false);
moveDown.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
//some code to make it wait here?
}
@Override
public void onAnimationEnd(Animation animation) {
image.setY((pxHeight / 2 - image.getHeight()));
displayScore(score);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
return moveDown;
}