2011-09-08 54 views
1

我需要實現以下功能:動畫開始,等到它完成,然後繼續執行代碼。問題是如何在主線程運行時「暫停」代碼執行。 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); 
+0

爲什麼你不能使用OnAnimationEnd? –

+0

AnimationListener的OnAnimationEnd()方法工作不正常。我不想重寫View的同一個方法,我只是需要最簡單的方法來讓代碼「等待」,直到線程正在做某事..... – bobi

+0

「工作不正常」太含糊。我建議你發佈你的代碼,並解釋發生了什麼,以及你想要發生什麼。 –

回答

0

嘗試使用的AsyncTask 這裏是它不會阻塞UI線程的例子

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> { 
    protected Long doInBackground(URL... urls) { 
     int count = urls.length; 
     long totalSize = 0; 
     for (int i = 0; i < count; i++) { 
      totalSize += Downloader.downloadFile(urls[i]); 
      publishProgress((int) ((i/(float) count) * 100)); 
     } 
     return totalSize; 
    } 

    protected void onProgressUpdate(Integer... progress) { 
     /*setProgressPercent(progress[0]);*/ 
    } 

    protected void onPostExecute(Long result) { 
     /* call continute method here */ 
    } 
} 

SR:http://developer.android.com/reference/android/os/AsyncTask.html

+0

我已經閱讀過這篇文章,但如果我錯了,請糾正我的錯誤 - 這是在後臺運行某些東西的方式 - 任務與主線程並行執行。我同意你的觀點 - 它不會阻止UI線程,但是「代碼執行」又如何呢?你可以根據具體情況更新你的答案:layout.startAnimation(GoDown)//持續時間500毫秒 - ?等待500毫秒? (使用AsyncTask)layout.addView()....恐怕這個AsyncTask會在後臺運行,下一行將立即執行...... – bobi

0

獨立後碰上它自己的方法,然後調用它的一部分立即或在適當的回調中。

public boolean onLongClick (View v) 
{ 

    if((v==textView2)&&(androidturn == false)&&(animationongoing == false)) 
     { 
      androidturn = true; 
      animationongoing = true; 
      inFromRightAnimation.setAnimationListener(new Animation.AnimationListener() 
      { 
       // Other listeners omitted. 
       public void onAnimationEnd(Animation anim) { 
       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; 
       styleImageButton(); 
       } 
      }); 
     L.startAnimation(inFromRightAnimation); 
    } 
    else 
    { 
     styleImageButton(); 
    } 
} 

private void styleImageButton() 
{   
    imageButton1.setBackgroundResource(R.color.Red); 
    imageButton1.playSoundEffect(0); 
} 

在異步代碼中,要避免強迫代碼等。

編輯:我解決了我的代碼使用onAnimationEnd。

+0

謝謝馬修。你提議我的是什麼我在第一個問題中提到:「我可以使用postDelayed(Runnable,miliseconds)並將所有代碼放入可運行狀態,但我不確定它是否是實現此目的的最佳方式。」如果只有兩行代碼 - 好吧,但想象一下,在動畫完成後必須執行3000行代碼。每300個我有另一個動畫,我必須等待它們完成。我仍然不確定是否將所有代碼放入很多延遲的可運行的是最好的方式。遺憾的是,似乎在Android中沒有其他方式來等待代碼 – bobi

+0

@bobi,我實際上並不想使用'postDelayed'。當我複製你的代碼時,這是一個疏忽。我不會推薦使用這個超時。我的意思是使用[onAnimationEnd](http://developer.android.com/reference/android/view/animation/Animation.AnimationListener.html#onAnimationEnd%28android.view.animation.Animation%29)偵聽器。之後的線路數量並不重要。對於3000行,您仍然可以擁有一個主要方法,並根據需要分成(調用)其他許多方法。 –