在Android开发中,我使用以下代码从网址加载drawable:
public Drawable getImageFromURL(String sURL) {
Object content = null;
try {
URL url = new URL(sURL);
content = url.getContent();
} catch (Exception ex) {
ex.printStackTrace();
}
InputStream is = (InputStream) content;
return Drawable.createFromStream(is, "src");
}
但是我想在加载drawable时显示这个ProgressBar:
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
progressBar = ProgressDialog.show(TabMenu.this, getString(R.string.name), getString(R.string.specials));
progressBar.setIcon(R.drawable.icon_template);
我该怎么做?我在Activity的onCreate()方法中调用了getImageFromURL()。 谢谢你的帮助!
答案 0 :(得分:1)
您需要在新主题中下载图像。您可以使用AsyncTask来简化此example,这也实现了进度条
答案 1 :(得分:1)
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
// do something
switch (msg.what){
case 1:
//ALL FUNCTION(S) -----------------------------
//-----------------------------------------------------
//-----------------------------------------------------
p_dialog.dismiss();
break;
// if error
case 0:
Toast.makeText(aktiviti.this, "Error", Toast.LENGTH_SHORT).show();
p_dialog.dismiss();
break;
}
}
};
Button new_button = (Button) findViewById(R.id.new_button);
new_button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
p_dialog = ProgressDialog.show((activity_name).this, "", "Loading Image...");
new Thread(new Runnable() {
public void run() {
processThread();
}}).start();
}
private void processThread() {
try{
handler.sendEmptyMessage(1);
}
catch (Exception e){
handler.sendEmptyMessage(0);
}
}
}
});
祝你好运!