0

我需要在 android 上进行一些涉及逐像素编辑的图像处理。这需要一点时间,屏幕只是冻结在那里,所以我想做一个加载对话框,让用户知道程序仍在运行。

这个editPhoto函数是我需要在后台运行的。

 private void editPhoto() {
    if (editPhotoImg.getDrawable() == null) {
        Toast.makeText(getApplicationContext(), "No Photo Selected",
                Toast.LENGTH_SHORT).show();
    } else if (hasSample == false) {
        Toast.makeText(getApplicationContext(), "No Sample Photo Selected",
                Toast.LENGTH_SHORT).show();
    } else {


        detectFaces(false);


        BitmapDrawable ebm = (BitmapDrawable) editPhotoImg.getDrawable();

        Bitmap editTemp = ebm.getBitmap();
        PointF editFaceMidPoint = getFaceMidPoint(editTemp);

        BitmapDrawable sbm = (BitmapDrawable) samplePhotoImg.getDrawable();
        Bitmap sampleTemp = sbm.getBitmap();
        PointF sampleFaceMidPoint = getFaceMidPoint(sampleTemp);

        if (editFaceMidPoint != null && sampleFaceMidPoint != null) {
            int editFaceMidPointPixel = editTemp.getPixel(
                    (int) editFaceMidPoint.x, (int) editFaceMidPoint.y);
            int sampleFaceMidPointPixel = sampleTemp.getPixel(
                    (int) sampleFaceMidPoint.x, (int) sampleFaceMidPoint.y);

            editPhotoImg.setImageBitmap(shiftRGB(editTemp,
                    editFaceMidPointPixel, sampleFaceMidPointPixel));

            savePhoto();

            detectFaces(true);
        }
    }

}

这是我的 AsyncTask 代码,它没有达到我想要的效果。它给了我一个我什至无法识别的错误。有人能告诉我我应该做什么,或者我应该在哪里调用 editPhoto() 方法,以便它在加载对话框后面而不是在加载对话框之前或之后运行?

     private class LoadEditPhoto extends AsyncTask<Void, Integer, Void> {
    // Before running code in separate thread

    @Override
    protected void onPreExecute() {
        /* Create a new progress dialog
        progressDialog = new ProgressDialog(EditPhoto.this);
        // Set the progress dialog to display a horizontal progress bar
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        // Set the dialog title to 'Loading...'
        progressDialog.setTitle("Loading...");
        // Set the dialog message to 'Loading application View, please
        // wait...'
        progressDialog.setMessage("Loading application View, please wait...");
        */
        progressDialog = ProgressDialog.show(EditPhoto.this,"Loading...",  
                "Editing Photo, please wait...", false, false);  
        // This dialog can't be canceled by pressing the back key
        progressDialog.setCancelable(false);
        // This dialog isn't indeterminate
        progressDialog.setIndeterminate(false);
        // The maximum number of items is 100
        progressDialog.setMax(100);
        // Set the current progress to zero
        progressDialog.setProgress(0);
        // Display the progress dialog
        progressDialog.show();
    }

    // The code to be executed in a background thread.
    @Override
    protected Void doInBackground(Void... params) {
        /*
         * This is just a code that delays the thread execution 4 times,
         * during 850 milliseconds and updates the current progress. This is
         * where the code that is going to be executed on a background
         * thread must be placed.
         * 
         */


        editPhoto();
        publishProgress(100);
        return null;
    }

    // Update the progress
    @Override
    protected void onProgressUpdate(Integer... values) {
        // set the current progress of the progress dialog
        progressDialog.setProgress(values[0]);
    }

    // after executing the code in the thread
    @Override
    protected void onPostExecute(Void result) {
        // close the progress dialog
        progressDialog.dismiss();

    }
}
4

2 回答 2

1

Put your editPhoto() in Thread .

Now define following code to handle multiple messages.

public enum WhatAbout {
    START,STOP
}

public WhatAbout[] wa = WhatAbout.values();

Then use Handler, which helps you to communicate BackgroundThread and UIThread. Without using handler, you can't change UI.

Put following code in your onCreate method.

handler = new Handler() {
  @Override
  public void handleMessage(Message msg) {
    if (msg.what < wa.length) {
    switch (wa[msg.what]) 
    {
    case START:
         progressDialog = ProgressDialog.show(getActivity(), null,
                        "Editing Photo... Please Wait...");
         break;

    case STOP:
         if (progressDialog.isShowing())
            progressDialog.dismiss();

         imageView.setImageBitmap(editedBitmap);
         break;
    }
  }
};

Then when your editing started in Thread, place following code at the first line of editPhoto().

handler.sendMessage(handler.obtainMessage(
                WhatAbout.START.ordinal(), 0, 0));

When your editing completed, place following code at the end of editPhoto().

handler.sendMessage(handler.obtainMessage(
                WhatAbout.STOP.ordinal(), 0, 0));
于 2013-01-21T10:24:46.443 回答
1

删除吐司消息,它应该可以工作。您无法从doInBackground方法更新 UI。除此之外,您的代码看起来不错。所以这应该是错误,。

或使用 runOnUi() 显示您的 Toast 消息。

像这样包围你的吐司,

ActivityObject.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            Toast.makeText(this, message, Toast.LENGTH_LONG).show();
        }
    });
于 2013-01-21T10:06:41.210 回答