我正在移动图像,我想在对象的动画完成后播放声音文件 图像移动但我尝试使用线程等待一段时间但它不起作用。
Animation animationFalling = AnimationUtils.loadAnimation(this, R.anim.falling);
iv.startAnimation(animationFalling);
MediaPlayer mp_file = MediaPlayer.create(this, R.raw.s1);
duration = animationFalling.getDuration();
mp_file.pause();
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(duration);
mp_file.start();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
感谢。
答案 0 :(得分:7)
您可以注册动画的代表:
animationFalling.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
// here you can play your sound
}
);
您可以阅读有关AnimationListener here
的更多信息答案 1 :(得分:-1)
建议你
awaitCompletion()
completionMonitor
字段来跟踪完成情况,在其上进行同步,并使用wait() and notifyAll()
协调awaitCompletion()代码段:
final class Animation {
final Thread animator;
public Animation()
{
animator = new Thread(new Runnable() {
// logic to make animation happen
});
}
public void startAnimation()
{
animator.start();
}
public void awaitCompletion() throws InterruptedException
{
animator.join();
}
}
您还可以将ThreadPoolExecutor
与单个线程或ScheduledThreadPoolExecutor
一起使用,并将动画的每个帧捕获为Callable。提交Callables序列并使用invokeAll() or a CompletionService
来阻止您感兴趣的线程,直到动画完成。