运行后台操作Android时显示加载对话框

运行后台操作Android时显示加载对话框,android,background,android-asynctask,loading,Android,Background,Android Asynctask,Loading,我需要在android上做一些图像处理,包括逐像素编辑。这需要一点太长,屏幕只是冻结在那里,所以我想做一个加载对话框,让用户知道程序仍在运行 这个editPhoto函数是我需要在后台运行的 private void editPhoto() { if (editPhotoImg.getDrawable() == null) { Toast.makeText(getApplicationContext(), "No Photo Selected",

我需要在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();

    }
}
私有类LoadEditPhoto扩展异步任务{
//在单独的线程中运行代码之前
@凌驾
受保护的void onPreExecute(){
/*创建一个新的进度对话框
progressDialog=新建progressDialog(EditPhoto.this);
//将“进度”对话框设置为显示水平进度条
progressDialog.setProgressStyle(progressDialog.STYLE_水平);
//将对话框标题设置为“正在加载…”
progressDialog.setTitle(“加载…”);
//请将对话框消息设置为“正在加载应用程序视图”
//等等……”
setMessage(“正在加载应用程序视图,请稍候…”);
*/
progressDialog=progressDialog.show(EditPhoto.this,“正在加载…”,
“正在编辑照片,请稍候…”,false,false);
//按后退键无法取消此对话框
progressDialog.setCancelable(假);
//这个对话框不是不确定的
progressDialog.setUndeterminate(false);
//项目的最大数量为100个
progressDialog.setMax(100);
//将当前进度设置为零
progressDialog.setProgress(0);
//显示进度对话框
progressDialog.show();
}
//要在后台线程中执行的代码。
@凌驾
受保护的Void doInBackground(Void…参数){
/*
*这只是一个延迟线程执行4次的代码,
*并更新当前进度。这是
*将在后台执行的代码
*线必须放好。
* 
*/
编辑照片();
出版进度(100);
返回null;
}
//更新进度
@凌驾
受保护的void onProgressUpdate(整型…值){
//设置“进度”对话框的当前进度
progressDialog.setProgress(值[0]);
}
//在线程中执行代码之后
@凌驾
受保护的void onPostExecute(void结果){
//关闭“进度”对话框
progressDialog.disclose();
}
}

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

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

像这样围绕着你的吐司

ActivityObject.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            Toast.makeText(this, message, Toast.LENGTH_LONG).show();
        }
    });
将您的
editPhoto()
放入线程

现在定义以下代码来处理多条消息

public enum WhatAbout {
    START,STOP
}

public WhatAbout[] wa = WhatAbout.values();
然后使用,它可以帮助您与
BackgroundThread
UIThread
进行通信。 如果不使用处理程序,则无法更改
UI

将以下代码放入
onCreate
方法中

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;
    }
  }
};
编辑完成后,将以下代码放在
editPhoto()的末尾


安德罗,谢谢你的建议。但不知怎么的,这对我来说仍然不起作用。由于您使用了应用程序上下文,因此它会崩溃并出现错误hmmits。而是将活动的上下文用于祝酒词。类似这样的东西,Toast.makeText(ActivityName.this,message,Toast.LENGTH_LONG.show();金丹,谢谢你的善意解释!但作为android编程的新手,我仍然无法实现它,尽管你一步一步地描述了这一点。我又把代码贴在下面了
handler.sendMessage(handler.obtainMessage(
                WhatAbout.START.ordinal(), 0, 0));
handler.sendMessage(handler.obtainMessage(
                WhatAbout.STOP.ordinal(), 0, 0));