我需要实现以下功能:动画开始,等到它完成,然后继续执行代码。问题是如何在主线程运行时“暂停”代码执行。 OnAnimationEnd()
方法不适合。如何让“代码执行”等待主线程?
我可以使用postDelayed(Runnable r, long milliseconds)
并将我的所有代码放入可运行状态,但我不确定这是否是执行此操作的最佳方式。还尝试使用thread.sleep()
,this.wait()
和另一个可运行threadnew.sleep()
的线程实例,但我没有看到所需的行为:看起来wait和sleep会停止动画和代码执行,而不仅仅是代码执行。
public boolean onLongClick (View v)
{
if((v==textView2)&&(androidturn == false)&&(animationongoing == false))
{
androidturn = true;
animationongoing = true;
L.startAnimation(inFromRightAnimation);
L.postDelayed(new Runnable() {
public void run() {
L.clearAnimation();
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.FILL_PARENT);
params.gravity = 0x50;
params.height =710;
L.setLayoutParams(params);
//L.layout(0,top,800,bottom);
animationongoing = false;
}
}, 500);
//here I need to stop the code execution for 500ms for animation to finish
imageButton1.setBackgroundResource(R.color.Red);
imageButton1.playSoundEffect(0);
为什么你不能使用OnAnimationEnd? –
AnimationListener的OnAnimationEnd()方法工作不正常。我不想重写View的同一个方法,我只是需要最简单的方法来让代码“等待”,直到线程正在做某事..... – bobi
“工作不正常”太含糊。我建议你发布你的代码,并解释发生了什么,以及你想要发生什么。 –