Android-等待动画完成再继续吗?
我花了很长时间尝试找出在继续代码之前如何等待动画完成。
到目前为止,我已经尝试了while(animation.hasEnded()== False),使用Thread.sleep,使用计时器,但似乎什么也没用。
I've spend quite a while trying to find out how to wait for an animation to finish before continuing the code. So far I've tried while(animation.hasEnded() == False), using Thread.sleep, using a timer but can't seem to get anything to work.
出现了很多次的事情是在动画中添加了一个动画侦听器,我已经尝试过,但是不确定如何进一步发展。
Something that cropped up quite a few times was to add an Animation Listener to the animation, which I've tried but not sure how to progress this further.
我创建动画,开始动画,然后有一行代码,我只想在动画完成后执行:
I create the animation, start the animation and then have a line of code after, which I only want to be executed after the animation has finished:
Animation moveDown = allAnimStuff(duration); //creates animation
image.startAnimation(moveDown); // Start the animation
displayScore(score); // wait for animation to finish before executing this line
,这是使用的allAnimStuff(duration)方法创建动画:
and here is the allAnimStuff(duration) method used to create the animation:
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;
}
在onAnimationEnd方法中编写以下行
write following line in onAnimationEnd method
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;
}