程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> android-加載圖像來顯示加載的進度

android-加載圖像來顯示加載的進度

編輯:編程綜合問答
加載圖像來顯示加載的進度

我使用下面的函數來顯示圖像:

Bitmap imageBitmap = loadBitmap(URL);

loadBitmap()如下:

private Bitmap loadBitmap(String url) {
try {
Bitmap bm = BitmapFactory.decodeStream((InputStream)this.fetch(url));
return bm;
}
catch(Exception e) {
e.printStackTrace();
return null;
}
}

fetch() :

public Object fetch(String address) {
try {
URL url = new URL(address);
Object content = url.getContent();
return content;
}
catch(Exception e) {
e.printStackTrace();
}
return this;
}

在加載的時候我想顯示加載進度或一個 load.png。並且在圖像加載結束時候顯示。如何實現? 我想創建函數 ProgressDialog,但是如何使用呢?

最佳回答:


你可以使用 AsyncTask 在 PreExecute() 方法中顯示 Progress Dialog,然後在 PostExecute() 方法中隱藏。

ProgressDialog prog = new ProgressDialog(this); // Create Progress Dialog
private class DownloadBitmap extends AsyncTask<Void, Integer, Void>{
    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        //Display progressDialog before download starts
        prog.show();
    }
    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        prog.hide(); //Hide Progress Dialog else use dismiss() to dismiss the dialog
    }
    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        /*
         * Perform download and Bitmap conversion here
         * 
         */
        return null;
    }
}

再通過下面代碼來調用 AsyncTask。

DownloadBitmap dd = new DownloadBitmap();
dd.execute();
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved