2013-03-18 30 views
3

我沒有太大的成功這樣做,我認爲會工作,所以我會問專家。在對話框中顯示圖片的變化循環?

我有一個鏈接到圖像的10個URL的ArrayList。我想要顯示第一個網址2秒,然後獲取第二個網址並執行相同的操作直到結束。

這是我到目前爲止,我想也許我不是在postExecute對話?:

private class LiveView extends AsyncTask<String, Integer, ArrayList<String>> { 
     ProgressDialog dialog; 
     private volatile boolean running = true; 

     @Override 
     protected void onPreExecute() { 
      dialog = ProgressDialog.show(
         myView.this, 
         "Working", 
         "Info message . . .", 
         true, 
         true, 
         new DialogInterface.OnCancelListener(){ 

          public void onCancel(DialogInterface dialog) { 
           cancel(true);       
          } 
         } 
       ); 
      } 

     @Override 
     protected void onCancelled() { 
       running = false; 
     } 

     @Override 
     protected ArrayList<String> doInBackground(String... passed) { 



      while (running) { 

      //removed the code here that sends the request to to make this shorter the server but it works fine 
      return responseFromServer.arrayListofURLs;   //list or URLs 

      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(ArrayList<String> listURLs) {  
      dialog.cancel(); 

      Dialog liveView = new Dialog(myView.this, R.style.Dialog); 
      liveView.setContentView(R.layout.liveview_dialogue); 
      TextView title = (TextView)liveView.findViewById(R.id.liveViewTitle);   
      Button button = (Button) liveView.findViewById(R.id.liveViewButton); 
      ImageView trackImage = (ImageView)liveView.findViewById(R.id.liveViewImage); 

      //I want to loop through the ten images here? 

      button.setOnClickListener(new OnClickListener() { 

       public void onClick(View v) { 

       } 
      }); 
      liveView.show(); 



     } 

    } 

回答

0

要完成一些代碼的答案,這裏是我如何處理它。我不需要傳遞任何變量,因爲我爲AsyncTask創建了全局位圖的ListArray。如果關閉對話框,我還使用布爾值來結束處理程序。

 liveView = new Dialog(myView.this, R.style.Dialog); 
     liveView.setContentView(R.layout.liveview_dialogue); 
     TextView title = (TextView)liveView.findViewById(R.id.liveViewTitle);   
     Button button = (Button) liveView.findViewById(R.id.liveViewButton); 
     trackImage = (ImageView)liveView.findViewById(R.id.liveViewImage); 





     button.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       run = false; 
       liveView.dismiss(); 
      } 
     }); 
     liveView.show(); 

     final Handler handler = new Handler(); 
     final Runnable r = new Runnable() 
     { 
      Iterator<Bitmap> it = images.iterator(); 
      public void run() 
      { 
       if(run){ 
       trackImage.setImageBitmap(it.next()); 
       if(it.hasNext()) 
       handler.postDelayed(this, 5000); 
       } 
      } 
     }; 
     handler.postDelayed(r, 5000); 
1

創建一個處理程序,並用它發送一個消息要關於它的最好辦法通過postDelayed延遲2秒。每當你收到消息時,通過調用trackImage.setImage顯示下一張圖片。當它們最終關閉對話框時,從處理程序中刪除所有待處理的消息。