2011-11-14 45 views
0

我正在繪製畫布上保存圖像。 保存到SD卡需要幾秒鐘的時間。 因此,在保存過程中,我想顯示圖像獲取保存的對話框...如何在這裏實現Progressdialog?

那麼如何實現進度對話框呢?

的圖像將代碼保存是象下面這樣:

case R.id.saveBtn: 
      Toast.makeText(getApplicationContext(), "Save", Toast.LENGTH_SHORT).show(); 
      final Activity currentActivity = this; 
      Handler saveHandler = new Handler(){ 
       @Override 
       public void handleMessage(Message msg) { 
        final AlertDialog alertDialog = new AlertDialog.Builder(currentActivity).create(); 
        alertDialog.setTitle("Drawing App"); 
        alertDialog.setMessage("Your drawing had been saved :)"); 
        alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int which) { 
          return; 
         } 
        }); 
        alertDialog.show(); 
       } 
      } ; 

      new ExportBitmapToFile(this,saveHandler, drawingSurface.getBitmap()).execute(); 

     break; 

回答

2

由於使用AsyncTaskExportBitmapToFile)可以調用preExecute()ProgressDialogpostExecute方法其關閉。要做到這一點,不需要Handler

編輯

class ExportBitmapToFile extends AsyncTask<...> { 
    private ProgressDialog m_progressDialog = null; 
    .... 

    @Override 
    protected void onPreExecute(){ 
     m_progressDialog = new ProgressDialog(m_context); 
     m_progressDialog.setMessage("please wait..."); 
     m_progressDialog.setCancelable(false); 
     m_progressDialog.show(); 
    } 

    @Override 
    protected void onPostExecute(HashMap<String, String> result){ 
     m_progressDialog.dismiss(); 
     //your alert dialog with message to user 
     AlertDialog.Builder builder = new AlertDialog.Builder(currentActivity); 
      builder.setTitle("Drawing App"); 
      builder.setMessage("Your drawing had been saved :)"); 
      builder.setButton("OK", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        dialog.dismiss(); 
        return; 
       } 
      }); 
     alertDialog = builder.create() 
     alertDialog.show(); 
    } 
} 
+0

感謝您的回覆。所以,在我的代碼中,我必須開始啓動進度對話框,並在哪裏解僱它? –

+0

@iDroidExplorer編輯我的答案與samle – Vladimir

+0

謝謝。它工作很好,因爲我想要的。 –

1

它看起來像ExportBitmapToFile是的AsyncTask,在這種情況下調用saveHandler.sendEmptyMessage(0);onPreExecute()

你也呼籲在onPostExecute()savehandler.sendEmptyMessage(1),使alertDialog一個成員變量(而不是本地實例),你的活動,並在您handleMessage(Message msg);功能看msg.what - 0手段顯示對話框,1表示對話應該是隱。此外,您可能要清理對話框中的活動onStop

你也想呼籲create()之前設置的AlertDialog.Builder所有屬性:

private AlertDialog alertDialog = null; 
    Handler saveHandler = new Handler(){ 
     @Override 
     public void handleMessage(Message msg) { 
      if(msg.what == 0 && alertDialog != null) { 
       AlertDialog.Builder builder = new AlertDialog.Builder(currentActivity); 
       builder.setTitle("Drawing App"); 
       builder.setMessage("Your drawing had been saved :)"); 
       builder.setButton("OK", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         dialog.dismiss(); 
         return; 
        } 
       }); 
       alertDialog = builder.create() 
       alertDialog.show(); 
      } 
      else if(alertDialog != null){ 
       alertDialog.dismiss(); 
       alertDialog = null; 
      } 
     } 
    } ; 

注 - 這不顯示進度指示器 - 您應該在PreExecute上顯示ProgressDialog,解除它,然後在PostExecute上顯示上述AlertDialog(使用相同的技術)。

+0

感謝您的回覆。 –

1

你應該在你的ExportBitmapToFile AsyncTask類中使用onPreExecute和onPostExecute,所以你應該添加類似的東西到那個類。

@Override 
    protected void onPostExecute(Void result) { 
     // TODO Auto-generated method stub 
     if (progressDialog.isShowing()) { 
      progressDialog.dismiss(); 

     } 
     super.onPostExecute(result); 
    } 



    @Override 
    protected void onPreExecute() { 

     progressDialog = ProgressDialog.show(context, "Message", 
       "Loading"); 

     super.onPreExecute(); 

    } 
+0

感謝您的回覆。 –