0

我在ui上創建了一個用於動畫更改圖像的線程。點擊按鈕時會中斷,並設置固定的圖像。 但是如果setOmage在onClick中意外發生runOnUiThread中的setImage,它將是錯誤的圖像。如何確保在線程中斷後進入某個地方?

有沒有什麼辦法在這段代碼中可以確保從InterruptedException返回後經過某處。 (我不想在UI線程上再拖延)

請幫忙謝謝!

thread = new Thread(){ 
     @Override 
     public void run() { 
      while(!this.isInterrupted()){ 
       for(int i=0; i<imageList.size(); i++){ 
        if(getActivity()==null) return; 
        getActivity().runOnUiThread(new Runnable() { 
         public void run() { 
          ImageViewAnimatedChange(getActivity().getApplication(), 
             imgFunction, imageList.get(j), 0); 
         } 
        }); 
        try { 
         Thread.sleep(2000); 
        } catch (InterruptedException e) { 
         Log.e(TAG, e.toString()); 
         return; 
        } 
       } 
      } 
     } 
    }; 

//一次的onClick

     if(thread!=null) { 
          thread.interrupt(); // catch interruptException 
          thread = null; 
          imgFunction.setImageResource(R.drawable.selector); 
         } 

編輯: 我重寫這一部分的建議。

public class asyncTask extends AsyncTask<String, String, String>{ 
    @Override 
    protected String doInBackground(String... params) { 
     Log.d(TAG, "doInBackground"); 
     try { 
      Thread.sleep(2000); 
     } catch (InterruptedException e) { 
      Log.w(TAG, e.toString()); 
      return null; 
     } 

     final List<Bitmap> imageList = getImageList(); 
     if(imageList.isEmpty()) return null; 
     Log.d(TAG, "start while "); 

     while(!isCancelled()){ 
      Log.d(TAG, "while:" +Boolean.toString(isCancelled())); 

      for(int i=0; i<imageList.size()+1; i++){ 
       final int j=i; 
       Log.d(TAG, "for" + Integer.toString(j)); 

       if (j == imageList.size()) 
        publishProgress(「ok」); 
       else 
        publishProgress(Integer.toString(i)); 

       try { 
        Thread.sleep(2000); 
       } catch (InterruptedException e) { 
        Log.w(TAG, e.toString()); 
        return null; 
       } 
      } 
     } 
     Log.d(TAG, "while end"); 
     return null; 
    } 
    @Override 
    protected void onPostExecute(String result) { 
     super.onPostExecute(result); 
     QUtil.logE(TAG, "onPostExecute"); 
     imgFunction.setImageResource(R.drawable.selector); 
    } 

    @Override 
    protected void onProgressUpdate(String... value) { 
     super.onProgressUpdate(value); 
     Log.d(TAG, "onProgressUpdate" + value[0]); 
     if(getActivity()==null || isCancelled()) return; 
     if(value[0]==「ok」) 
      ImageViewAnimatedChange(getActivity().getApplication(), 
        imgFunction, null, R.drawable.selector); 
     else 
      ImageViewAnimatedChange(getActivity().getApplication(), 
        imgFunction, getImageList().get(Integer.parseInt(value[0])), 0); 
    } 

    @Override 
    protected void onCancelled() { 
     super.onCancelled(); 
     Log.e(TAG, "onCancelled"); 
     try { 
      Thread.sleep(60); 
     } catch (InterruptedException e) { 
      Log.w(TAG, e.toString()); 
     } 
     imgFunction.setImageResource(R.drawable.selector); 
    } 
} 

//開始

iftttAsyncTask = new IftttAsyncTask().execute(""); 

//結束的onClick

if(asyncTask!=null) { 
          asyncTask.cancel(true); // catch interruptException 
          threadIfttt = null; 
         } 

則有時工作有時不工作,而 「doInBackground」 是不是在睡覺胎面 不行!它沒有去onCancel,但沒有真正取消,我假以isCanceled() 日誌圖片如下 enter image description here

回答

0

你可能會更好使用AsyncTask這種類型的後臺工作,因爲它可以讓你執行單獨UI操作取決於任務是繼續還是中斷。

例如,在下面這個示例中AsyncTask

public class TestTask extends AsyncTask<String, Void, String> { 

    protected String doInBackground(String... args) { 

     String input = args[0]; 
     String output = "simulated return value"; 

     return output; 
    } 

    protected void onPostExecute(String result) { 
     //if your background operation succeeds, you 
     //would update your image here 
    } 

    protected void onCancelled() { 
     //if your task is cancelled, update your UI with the 
     //default image here 
    } 
} 

如果後臺操作成功,你會更新爲新圖像的UI在onPostExecute(),這在UI線程上運行。如果您中斷線程,例如點擊按鈕,您將取消該任務,而不是onPostExecute(),而是調用onCancelled()方法。此方法也在UI線程上運行,您可以使用默認圖像更新UI。

+0

嗯..與AsyncTask相同的問題, 和我需要在AsyncTask onProgressUpdate不onPostExecute,它會結束while線程。 這個問題發生在我做的void ImageViewAnimatedChange中,它使用了anim_in和anim_out,所以它仍然發生在我setImage(這沒有anim) –

相關問題