显示进度条,直到从服务器下载图像

时间:2011-05-23 04:57:25

标签: android progress-bar

亲爱的,我能够从网址显示图片,意味着网址存储在一个数组中,我有两个按钮“下一步”,“返回”,当我点击按钮图像时,一个接一个地看到图像从服务器下载响应现在我想要显示进度条,当图像从服务器下载时,当响应来时,进度条自动隐藏menas显示progreebar直到响应为图像不显示在屏幕上,当图像显示progreebar活动完成...所以如何做到这一点我在按钮点击一个接一个地显示图像的代码片段如下...请在我的代码中修改如果可能bcoz我是android和java中的新手..还有很多提前...

public class artspacedetailShowingNow extends Activity implements OnClickListener {
private int imageCounter = 0;
private ImageView imageLoader;
private String[] imageList = {"http://www.artealdiaonline.com/var/artealdia_com/storage/images/argentina/directorio/galerias/ruth_benzacar/artistas/martin_di_girolamo._diosas/198915-1-esl-AR/MARTIN_DI_GIROLAMO._Diosas.jpg","http://www.artealdiaonline.com/var/artealdia_com/storage/images/argentina/directorio/galerias/ruth_benzacar/artistas/jorge_macchi._la_espera/198929-1-esl-AR/JORGE_MACCHI._La_espera.jpg","http://www.artealdiaonline.com/var/artealdia_com/storage/images/argentina/directorio/galerias/ruth_benzacar/artistas/leon_ferrari._hongo_nuclear/198950-1-esl-AR/LEON_FERRARI._Hongo_Nuclear.jpg","http://www.artealdiaonline.com/var/artealdia_com/storage/images/argentina/directorio/galerias/ruth_benzacar/artistas/martin_sastre._fiebre/198922-1-esl-AR/MARTIN_SASTRE._Fiebre.jpg"};
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.showingnow);
       imageLoader = (ImageView) findViewById(R.id.imageLoader);
     Button next = (Button) findViewById(R.id.next);
    Button back = (Button) findViewById(R.id.back);
    next.setOnClickListener(this);
    back.setOnClickListener(this);
    back.setEnabled(false);
    this.loadImage(imageList[imageCounter]);

}
@Override
 public void onClick(View v)
{
String imagePath = null;
  switch (v.getId())
{
case R.id.next:
    Log.i("Tag","tag");
    if(imageCounter < imageList.length)
    {
        imageCounter++;
        imagePath = imageList[imageCounter];
        if (imageCounter==(imageList.length)-1)
        {
            { 
                Button next=(Button)findViewById(R.id.next); 
               next.setEnabled(false); 
             }
        }
        else
        {
            Button back=(Button)findViewById(R.id.back); 
            back.setEnabled(true); 

        }
    }
    break;
case R.id.back:
    if(imageCounter > 0)
    {
        imageCounter--;
        imagePath = imageList[imageCounter];
        if (imageCounter==0) 
        { 
            Button back=(Button)findViewById(R.id.back); 
            back.setEnabled(false); 

        }
        else
        {
            Button next=(Button)findViewById(R.id.next); 
             next.setEnabled(true);

        }
    }
    break;
}
this.loadImage(imagePath);
}
private void loadImage(String imagePath)
{   
  try {
      URL aURL = new URL(imagePath);
      URLConnection conn = aURL.openConnection();
      conn.connect();
      InputStream is = conn.getInputStream();
      BufferedInputStream bis = new BufferedInputStream(is);
     Bitmap bm = BitmapFactory.decodeStream(bis);

      imageLoader.setImageBitmap(bm);
      imageLoader.setImageBitmap(bm);
  } 

}

4 个答案:

答案 0 :(得分:3)

检查一下..

public class artspacedetailShowingNow extends Activity implements OnClickListener {
private int imageCounter = 0;
private ImageView imageLoader;
private ProgressDialog bar;
private String[] imageList = {"http://www.artealdiaonline.com/var/artealdia_com/storage/images/argentina/directorio/galerias/ruth_benzacar/artistas/martin_di_girolamo._diosas/198915-1-esl-AR/MARTIN_DI_GIROLAMO._Diosas.jpg","http://www.artealdiaonline.com/var/artealdia_com/storage/images/argentina/directorio/galerias/ruth_benzacar/artistas/jorge_macchi._la_espera/198929-1-esl-AR/JORGE_MACCHI._La_espera.jpg","http://www.artealdiaonline.com/var/artealdia_com/storage/images/argentina/directorio/galerias/ruth_benzacar/artistas/leon_ferrari._hongo_nuclear/198950-1-esl-AR/LEON_FERRARI._Hongo_Nuclear.jpg","http://www.artealdiaonline.com/var/artealdia_com/storage/images/argentina/directorio/galerias/ruth_benzacar/artistas/martin_sastre._fiebre/198922-1-esl-AR/MARTIN_SASTRE._Fiebre.jpg"};
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.showingnow);
       imageLoader = (ImageView) findViewById(R.id.imageLoader);
     Button next = (Button) findViewById(R.id.next);
    Button back = (Button) findViewById(R.id.back);
    next.setOnClickListener(this);
    back.setOnClickListener(this);
    back.setEnabled(false);
    new ImageDownload().execute(imageList[imageCounter]); 
    //this.loadImage(imageList[imageCounter]);

}
@Override
 public void onClick(View v)
{
String imagePath = null;
  switch (v.getId())
{
case R.id.next:
    Log.i("Tag","tag");
    if(imageCounter < imageList.length)
    {
        imageCounter++;
        imagePath = imageList[imageCounter];
        if (imageCounter==(imageList.length)-1)
        {
            { 
                Button next=(Button)findViewById(R.id.next); 
               next.setEnabled(false); 
             }
        }
        else
        {
            Button back=(Button)findViewById(R.id.back); 
            back.setEnabled(true); 

        }
    }
    break;
case R.id.back:
    if(imageCounter > 0)
    {
        imageCounter--;
        imagePath = imageList[imageCounter];
        if (imageCounter==0) 
        { 
            Button back=(Button)findViewById(R.id.back); 
            back.setEnabled(false); 

        }
        else
        {
            Button next=(Button)findViewById(R.id.next); 
             next.setEnabled(true);

        }
    }
    break;
}
 new ImageDownload().execute(imagePath);
//this.loadImage(imagePath);
}
private void loadImage(String imagePath)
{   
  try {
      URL aURL = new URL(imagePath);
      URLConnection conn = aURL.openConnection();
      conn.connect();
      InputStream is = conn.getInputStream();
      BufferedInputStream bis = new BufferedInputStream(is);
     Bitmap bm = BitmapFactory.decodeStream(bis);

      imageLoader.setImageBitmap(bm);
      imageLoader.setImageBitmap(bm);
  }

private class ImageDownload extends AsyncTask<String , Void, Void>(){

                        @Override
                        protected Void doInBackground(String... params) {
                            loadImage(params[0]);
                            return null;
                        }
                        @Override
                        protected void onPostExecute(Void result) {

                            bar.dismiss();
                            super.onPostExecute(result);
                        }
                        @Override
                        protected void onPreExecute() {
                            bar = new ProgressDialog(activity);
                            bar.setMessage("Processing...");
                            bar.setIndeterminate(true);
                            super.onPreExecute();
                        }

                    }
}

答案 1 :(得分:0)

在代码中使用Handler的概念来执行此操作。

这是代码..

ProgressDialog _progressDialog = ProgressDialog.show(this,"Saving Data","Please wait......");
settintAdater();

 private void settingAdater(){

        Thread _thread = new Thread(){

            public void run() {

                Message _msg = new Message();
                _msg.what = 1; 
                 // Do your task where you want to rerieve data to set in adapet
                YourCalss.this._handle.sendMessage(_msg);
            };
        };
        _thread.start();
    }
 Handler _handle = new Handler(){

        public void handleMessage(Message msg) {

            switch(msg.what){

                case 1:
                    _progressDialog.dismiss();
                     listview.setAdapter();
            }
        }
 }

答案 2 :(得分:0)

显示进度条,直到图像下载,尝试使用asyncTask方法。

这里是示例代码。

private class ImageDownload extends AsyncTask<String , Void, Void>(){

                        @Override
                        protected Void doInBackground(String... params) {
                            loadImage(params[0]);
                            return null;
                        }
                        @Override
                        protected void onPostExecute(Void result) {

                            bar.dismiss();
                            super.onPostExecute(result);
                        }
                        @Override
                        protected void onPreExecute() {
                            bar = new ProgressDialog(activity);
                            bar.setMessage("Processing...");
                            bar.setIndeterminate(true);
                            super.onPreExecute();
                        }

                    }

创建局部变量ProgressBar bar; 替换此行new ImageDownload().execute(imageURL);而不是loadImage(imageURL);

有关asyncTask的更多信息,请go here.

答案 3 :(得分:0)

检查图像何时从服务器下载,将ImageView和progressdialog放在同一个地方, 首先设置progressdialog visibile - &gt; true和imageview可见性 - &gt;假。然后开始从服务器下载图像,使用处理程序,并了解服务器何时可用的图像。那时候设置progressdialog的可见性 - &gt; false和imageview可见性 - &gt;真