[英]Android wait till animation is completed
I am moving an image and I want to play a sound file after the object's animation is completed 我正在移动图像,我希望在对象的动画完成后播放声音文件
The image moves but I tried using Threads to wait until a duration but it didn't work. 图像移动但我尝试使用线程等待一段时间但它不起作用。
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();
Thanks. 谢谢。
you can register a delegate for the animation: 你可以为动画注册一个代表:
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
}
);
you can read more about the AnimationListener here 你可以在这里阅读有关AnimationListener的更多信息
Suggest you 建议你
awaitCompletion()
awaitCompletion()
completionMonitor
field to track completion, synchronize on it, and use wait() and notifyAll()
to coordinate the awaitCompletion() completionMonitor
字段来跟踪完成,在其上进行同步,并使用wait() and notifyAll()
来协调awaitCompletion() Code snippet: 代码段:
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();
}
}
You could also use a ThreadPoolExecutor
with a single thread or ScheduledThreadPoolExecutor
, and capture each frame of the animation as a Callable. 您还可以将
ThreadPoolExecutor
与单个线程或ScheduledThreadPoolExecutor
,并将动画的每个帧捕获为Callable。 Submitting the sequence of Callables and using invokeAll() or a CompletionService
to block your interested thread until the animation is complete. 提交Callables序列并使用
invokeAll() or a CompletionService
来阻止您感兴趣的线程,直到动画完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.