我需要實現以下功能:動畫開始,等到它完成,然後繼續執行代碼。問題是如何在主線程運行時「暫停」代碼執行。 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
「工作不正常」太含糊。我建議你發佈你的代碼,並解釋發生了什麼,以及你想要發生什麼。 –