如何使“代码执行”等待主线程?

时间:2011-09-08 14:25:52

标签: android multithreading

我需要实现以下功能:动画启动,等到它完成,然后继续执行代码。问题是如何在主线程运行时“暂停”代码执行。 OnAnimationEnd()方法不合适。

我可以使用postDelayed(Runnable r, long milliseconds)并将所有代码放在runnable中,但我不确定这是执行此操作的最佳方法。还尝试使用thread.sleep()this.wait()和另一个带有runnable threadnew.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);

2 个答案:

答案 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

答案 1 :(得分: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。