2012-11-27 81 views
0

我在我的應用程序中有一個活動,它需要幾個屏幕捕獲並移動到下一個按鈕單擊活動。我想在截圖時向用戶展示一些進度或狀態(需要5-8秒)。我嘗試使用AsyncTaskrunOnUithread()。但沒有運氣。即使我在doInBackground()中編寫了整個代碼,我也不知道爲什麼ProgressDialog在獲取所有屏幕截圖後啓動。有什麼更好的方法來做到這一點?這裏是我的代碼:延遲截圖

public class TakeScreenShots extends AsyncTask<Void, Void, Void> { 
     private ProgressDialog dialog; 


      @Override 
      protected void onPreExecute() { 
       this.dialog = ProgressDialog.show(MyActivity.this, "MyTitle", 
         "Loading .....", true); 

      } 

      @Override 
      protected Void doInBackground(Void... params) { 
       MyActivity.this.runOnUiThread(new Runnable() { 

        @Override 
        public void run() { 
         takeScreenshots(); 

        } 
       }); 

       return null; 
      } 

      @Override 
      protected void onPostExecute(Void unused) { 
       gotoNextActivity(); 

       this.dialog.dismiss(); 

      } 

     } 
+0

如果你實現了asynctask,它應該顯示進度對話框,並在完成後移動到下一個活動。 – Raghunandan

+0

是的。但它不是那樣做的。在調用takeScreenShots()方法後出現進度對話框。 –

+0

你在NExt屏幕上做什麼? – Sameer

回答

0

我從一個答案的答案在this question。我在圖像視圖中設置了一個drawable。所以,我在Ui Thread上保留了該行,並保留在doInBackground()中。

+0

這我已經在我的評論中提到*見我已經給出一個例子,如果你想改變UI在'preExecute()'* –

1

不,你做錯了,在runOnUiThread代替捕獲直接捕獲它在doInBackground()

這裏是工作示例

public class backImageProcess extends AsyncTask<Void, Void, Void> 
    { 
private ProgressDialog dialog; 
     @Override 
     protected void onPreExecute() { 

      this.dialog = ProgressDialog.show(MyActivity.this, "MyTitle", 
         "Loading .....", true); 

      try { 
       File mFile= new File(mTempImagename); 
       if(mFile!=null) 
        mFile.delete(); 
      } catch (Exception e) { 
      } 

      super.onPreExecute(); 
     } 
     @Override 
     protected Void doInBackground(Void... params) { 
      mLayoutRoot.setDrawingCacheEnabled(true); 
      mLayoutRoot.buildDrawingCache(); 

      Bitmap mBitmap= mLayoutRoot.getDrawingCache(); 
      try { 
       if(mBitmap!=null) 
       { 
        FileOutputStream out = new FileOutputStream(mTempImagename); 
        mBitmap.compress(Bitmap.CompressFormat.PNG, 90, out); 
        out.flush(); 
        out.close(); 
       } 
      } catch (Exception e) { } 

      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      if(this.dialog!=null) 
       this.dialog.dismiss(); 

      gotoNextActivity(); 

     } 
    } 
+0

但我的代碼實際上有一個需要很多截圖的for循環。我的意思是一個ImageView的Drawable被改變並且截圖被拍攝。可能會有10到15個截圖。 –

+0

我試着讓我的代碼保留在doInBackground中,就像你做一次捕獲一樣。它不起作用 –

+0

它引發這個異常:android.view.ViewRoot $ CalledFromWrongThreadException –