Android - 如何使用Asynchtask的背景图像onPreExecute()方法显示ProgressBar

时间:2012-09-21 12:03:29

标签: android android-asynctask

我是android的新手。我正在为我的应用程序使用XML解析,如何显示背景图像,并在点击我的应用程序时它顶部显示ProgessBar并保持屏幕直到我的数据不会出现。我正在使我的应用程序像BBC新闻Android应用程序

AsyncTask<Void, Void, Drawable> downloader = new AsyncTask<Void, Void, Drawable>() {

    @Override
    protected Drawable doInBackground(Void... params) {
        AndroidHttpClient client = AndroidHttpClient.newInstance(context.getPackageName());
        try {
            HttpGet get = new HttpGet(url);
            final HttpParams httpParams = new BasicHttpParams();
            HttpClientParams.setRedirecting(httpParams, true);
            get.setParams(httpParams);
            HttpResponse resp = client.execute(get);
            int status = resp.getStatusLine().getStatusCode();
            if(status != HttpURLConnection.HTTP_OK){
                //Log.i(LOGTAG, "Couldn't download image from Server: " + url + " Reason: " + resp.getStatusLine().getReasonPhrase() + " / " + status);
                return null;
            }
            HttpEntity entity = resp.getEntity();
            //Log.i(LOGTAG, url + " Image Content Length: " + entity.getContentLength());
            InputStream is = entity.getContent();
            FileOutputStream fos = context.openFileOutput(filename, Context.MODE_PRIVATE);
            copyStream(is, fos);
            fos.close();
            is.close();
            FileInputStream  fis = context.openFileInput(filename);
            return loadDrawableFromStream(context, fis);
        }
        catch (Exception ex) {
            //Log.e(LOGTAG, "Exception during Image download of " + url, ex);
            return null;
        }
        finally {
            client.close();
        }
    }

    @Override
    protected void onPostExecute(Drawable result) {
        if (result == null)
            result = defaultDrawable;
        mPendingDownloads.remove(url);
        cache.put(url, result);
        for (ImageView iv: downloads) {
            // validate the url it is waiting for
            String pendingUrl = mPendingViews.get(iv);
            if (!url.equals(pendingUrl)) {
                //Log.i(LOGTAG, "Ignoring out of date request to update view for " + url);
                continue;
            }
            mPendingViews.remove(iv);
            if (result != null) {
                final Drawable newImage = result;

                Drawable newSize=resize(newImage);


                final ImageView imageView = iv;

                imageView.setImageDrawable(newSize);
            }
        }
    }

    private BitmapDrawable resize(Drawable newImage) {
        // TODO Auto-generated method stub

        Bitmap d = ((BitmapDrawable)newImage).getBitmap();
        Bitmap bitmapOrig = Bitmap.createScaledBitmap(d, 75, 75, false);
        return new BitmapDrawable(bitmapOrig);                                              
    }
};
downloader.execute();
}

private static Hashtable<ImageView, String> mPendingViews = new Hashtable<ImageView, String>();
private static Hashtable<String, ArrayList<ImageView>> mPendingDownloads = new Hashtable<String, ArrayList<ImageView>>();
}

2 个答案:

答案 0 :(得分:0)

private ProgressDialog progressDialog;

protected void onPreExecute() {
        progressDialog = ProgressDialog.show(context, "", context.getResources().getString(R.string.searching_wait_message), true);
}
protected void onPostExecute(Vector<SearchResult> result) {
    progressDialog.dismiss();
}

答案 1 :(得分:0)

的AsyncTask:

private ProgressDialog mDialog;

Override
protected void onPreExecute() {
     mDialog = new ProgressDialog(...);
     mDialog.setContentView(R.layout.dialog_layout);

     // define your mDialog object further...

     mDialog.show()
}

@Override
protected Drawable doInBackground(...) {
     // do stuff
}

@Override
protected void onPostExecute(...) {
     mDialog.dismiss();
}

dialog_layout.xml(Pseudo)

<LinearLayout
     background=...>

     <ProgressBar />

</LinearLayout>