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