2011-09-08 50 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行,您仍然可以拥有一个主要方法,并根据需要分成(调用)其他许多方法。 –