带有进度和 secondProgress 的 ProgressDialog

ProgressDialog with progress and secondProgress


我在我的应用程序中使用 AsyncTask 来下载多张图片。这个异步任务是在一个单独的 class 中实现的,我使用一个接口来更新 UI 线程。执行任务并更新进度时会显示一个 progressDialog。
到目前为止,我既可以显示当前的下载进度,也可以显示已下载的图片数量,我想同时进行这两项操作。
这是我的异步任务 class :

public class DownloadPicturesTask extends AsyncTask<String ,Integer , Boolean> {
/**
 * Interface updating the UI thread thanks to its methods
 */
public DownloadPicturesResponse handler = null;
private Activity callingActivity = null;
private String message;

public DownloadPicturesTask(Activity activity){
    attach(activity);
}

public void attach(Activity activity){
    this.callingActivity = activity;
}

public void detach(){
    this.callingActivity = null;
}

@Override
protected void onPreExecute() {
    handler.picturesPreExecute();
}

/**
 * For each picture, we store the downloaded file in the historic folder <br/>
 * Ff all pictures are successfully downloaded, return true <br/>
 * If fail or cancel return false.
 * @param urls list of picture names
 * @return boolean
 */
@Override
protected Boolean doInBackground(String... urls) {
    InputStream input;
    OutputStream output;
    HttpURLConnection connection;
    try{
        for (int i = 0 ; i < urls.length ;i++ ) {
            String url = urls[i];
            URL newURL = new URL(Config.URL_PICTURES + url);
            connection = (HttpURLConnection) newURL.openConnection();
            connection.connect();
            if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
                message = connection.getResponseCode() + " : " + connection.getResponseMessage();
                return Boolean.FALSE;
            }
            int fileLength = connection.getContentLength();
            input = connection.getInputStream();
            File historicFolder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), Config.HISTORIC_DIRECTORY);
            if (!historicFolder.exists()) {
                boolean mkdir = historicFolder.mkdir();
                if (!mkdir) {
                    message = "Impossible de créer le dossier historique.";
                    return Boolean.FALSE;
                }
            }
            output = new FileOutputStream(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/" + Config.HISTORIC_DIRECTORY + "/" + url);
            byte data[] = new byte[4096];
            long total = 0;
            int count;
            while ((count = input.read(data)) != -1) {
                if (isCancelled()) {
                    input.close();
                    message = "Téléchargement annulé";
                    return Boolean.FALSE;
                }
                total += count;
                /* here is where i publish progress  */
                if (fileLength > 0)
                    publishProgress((int) (total * 100 / fileLength),i);
                output.write(data, 0, count);
            }
            output.flush();
            output.close();
            input.close();
        }
    }catch (Exception e){
        e.printStackTrace();
        Log.e(Config.LOG_ERROR, "MeasurePictureTask", e);
        message = e.toString();
        return Boolean.FALSE;
    }
    message = "Téléchargement terminé";
    return Boolean.TRUE;
}

/**
 * Calling the handler concerned method
 * @param values integer value of the progress
 */
@Override
protected void onProgressUpdate(Integer... values) {
    handler.picturesOnProgressUpdate(values[0],values[1]);
}

/**
 * Calling the handler concerned method
 * @param succeed boolean
 */
@Override
protected void onPostExecute(Boolean succeed) {
    if(callingActivity != null)
        handler.picturesOnPostExecute(succeed,message);
    else {
        Log.e(Config.LOG_ERROR,"DownloadPicture Task : Calling Activity has been lost.");
    }
}

这里调用 activity(只有 asyncTask 部分):

@Override
public void picturesPreExecute() {
    pDial = new ProgressDialog(this);
    pDial.setMessage("Chargement des photos");
    pDial.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    pDial.setMax(pictures.size());
    pDial.setCancelable(true);
    pDial.setOnCancelListener(new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            pictureTask.cancel(true);
        }
    });
    pDial.show();
}

@Override
public void picturesOnProgressUpdate(int progress,int nbPictures) {
    pDial.setSecondaryProgress(progress);
    pDial.setProgress(nbPictures);
}

@Override
public void picturesOnPostExecute(boolean succeed, String result) {
    pDial.dismiss();
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
    if(succeed)
        Toast.makeText(this,R.string.t_picture_download_succeed,Toast.LENGTH_SHORT).show();
    else
        Toast.makeText(this,result,Toast.LENGTH_SHORT).show();
}

我想要这种显示方式,实际下载的百分比值,以及已经下载图片的数值:

为此,您需要创建新的 class 来扩展 progessBar。详情请查看http://colintmiller.com/how-to-add-text-over-a-progress-bar-on-android/

您应该创建一个布局,其中包含下载百分比的进度条和每张图片增加的 TextView。然后膨胀一个片段的布局