如何使用Volley / Glide / Picasso或其他方式显示进度条,同时从服务器

时间:2018-01-03 08:59:19

标签: android android-volley picasso android-glide image-loading

我想使用Volley / Glide / Picasso或其他网络电话库显示进度条(显示百分比)。有很多解决方案,但不是Volley / Glide / Picasso,我如何在下载之前获得文件图像大小,并且剩余时间大小以便我可以使用进度条显示。

使用毕加索。

Picasso.with(MainActivity.this).load(server_url).into(image_download);

使用Glide

 GlideApp
.with(myFragment)
.load(url)
.centerCrop()
.placeholder(R.drawable.loading_spinner)
.into(myImageView);

2 个答案:

答案 0 :(得分:1)

使用滑行

尝试此操作
progressBar = (ProgressBar)findViewById(R.id.progressBar);
progressBar.setVisibility(View.VISIBLE);
        Glide.with(context)
                .load(imageUrl)
                .listener(new RequestListener<String, GlideDrawable>() {
                    @Override
                    public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                        progressBar.setVisibility(View.GONE);
                        return false;
                    }
                })
                .into(imageview);

Picasso

Picasso.with(context)
            .load(imageUrl)
            .into(imageview, new com.squareup.picasso.Callback() {
                        @Override
                        public void onSuccess() {
                            //do something when picture is loaded successfully
                             progressBar.setVisibility(View.GONE);

                        }

                        @Override
                        public void onError() {
                            //do something when there is picture loading error
                        }
                    });

AsyncTask

public class DownloadImage extends AsyncTask<String, Integer, Bitmap> {
    ImageView bmImage;
    ProgressBar progressBar;

    public DownloadImage(ImageView bmImage, ProgressBar progressBar) {
        this.bmImage = bmImage;
        this.progressBar = progressBar;
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.d("Error", e.getStackTrace().toString());

        }
        return mIcon11;
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        progressBar.setProgress(values[0]);
    }

    protected void onPostExecute(Bitmap result) {
        bmImage.setImageBitmap(result);
    }
}

像这样使用

 new DownloadImage(imamgeview,progressBar).execute(url);

答案 1 :(得分:0)

您可以使用目标ImageView和进度条创建布局。加载开始/结束后,您可以隐藏或显示您的进度。 其他示例link