Set visibility of progress bar gone on completion of image loading using Glide library

前端 未结 12 1999
栀梦
栀梦 2020-12-04 11:04

Hi I want to have a progress bar for image which will shown while image loading but when image loading will be completed I want to set it to gone. Earlier I was using Picass

相关标签:
12条回答
  • 2020-12-04 11:37

    This is the best answer as it does not use any hack like setting visibility to get the desired output.

    Download a gif of progressbar and call it progressbargif and put it in the drawable folder.

            Glide.with(ctx)
                .load(url)
                .thumbnail(Glide.with(ctx).load(R.drawable.progressbargif))
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .error(R.drawable.image_unavailable)
                .crossFade(200)
                .into(iv);
    

    Once the url image is loaded, the thumbnail vanishes. The thumbnail vanishes immediately when the cached image is loaded.

    0 讨论(0)
  • 2020-12-04 11:40

    How I did things. the shorter way, cleaner code

    example:

    progress_bar.visibility = View.VISIBLE
    
    profilePicturePath?.let {
        GlideApp.with(applicationContext)
            .load(CloudStorage.pathToReference(it))
            .placeholder(R.drawable.placeholder)
            .listener(GlideImpl.OnCompleted {
                progress_bar.visibility = View.GONE
            })
        .into(profile_picture)
    } ?: profile_picture.setImageResource(R.drawable.placeholder)
    

    usage:

    GlideImpl.OnCompleted {
        // completed
    }
    

    just pass GlideImpl.OnCompleted { } to the Glide's .listener()

    GlideImpl.kt class that accepts Glide's RequestListener

    import android.graphics.drawable.Drawable
    import com.bumptech.glide.load.DataSource
    import com.bumptech.glide.load.engine.GlideException
    import com.bumptech.glide.request.RequestListener
    import com.bumptech.glide.request.target.Target
    
    object GlideImpl {
    
        object OnCompleted : RequestListener<Drawable> {
    
            private lateinit var onComplete: () -> Unit
    
            operator fun invoke(onComplete: () -> Unit): OnCompleted {
                OnCompleted.onComplete = { onComplete() }
                return this
            }
    
            override fun onResourceReady(
                resource: Drawable?,
                model: Any?,
                target: Target<Drawable>?,
                dataSource: DataSource?,
                isFirstResource: Boolean
            ): Boolean {
                onComplete()
                return false
            }
    
            override fun onLoadFailed(
                e: GlideException?,
                model: Any?,
                target: Target<Drawable>?,
                isFirstResource: Boolean
            ): Boolean {
                onComplete()
                return false
            }
        }
    }
    

    and that is it!

    0 讨论(0)
  • 2020-12-04 11:42
    1. In xml take progress bar with height & width(match_parent).
    2. Before call below mention method , set progress bar visibility Visible.

      public void setImageWIthProgressBar(Context context, final ImageView imageView, String imageUrl, final ProgressBar progressBar) {
      
              Glide.with(context)
                      .load(imageUrl)
                      .listener(new RequestListener<String, GlideDrawable>() {
                          @Override
                          public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                              progressBar.setVisibility(View.GONE);
                              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);
      
          }//setImageWIthProgressBar
      
    0 讨论(0)
  • 2020-12-04 11:46

    Question is rather old, and I don't know what was the situation with glide in those times, but now it can be easily done with listener (not as proposed in the answer chosen as correct).

    progressBar.setVisibility(View.VISIBLE);
    Glide.with(getActivity())
         .load(args.getString(IMAGE_TO_SHOW))
         .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(imageFrame)
    ;
    

    You return true if want to handle things like animations yourself and false if want glide to handle them for you.

    0 讨论(0)
  • 2020-12-04 11:47

    GlideDrawable are deprecated, use simple Drawable

    RequestOptions requestOptions = new RequestOptions();
    requestOptions.placeholder(R.drawable.placeholder);
    requestOptions.error(R.drawable.error);
    
    Glide.with(getContext())
                     .setDefaultRequestOptions(requestOptions)
                     .load(finalPathOrUrl)
                     .listener(new RequestListener<Drawable>() {
                            @Override
                            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                                progressBar.setVisibility(View.GONE);
                                return false;
                            }
    
                            @Override
                            public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                                progressBar.setVisibility(View.GONE);
                                return false;
                            }
                        })
                     .into(mImageView);
    
    0 讨论(0)
  • 2020-12-04 11:50

    The above solution works pretty well for me too but when i use asBitmap() to download the image. It does not work.

    We need to use BitmapImageViewTarget

    Glide.with(this) .load(imageURL)
     .asBitmap()
     .placeholder(R.drawable.bg)
     .into(new BitmapImageViewTarget(imageView) {
                @Override
                public void onResourceReady(Bitmap  drawable, GlideAnimation anim) {
                    super.onResourceReady(drawable, anim);
                    progressBar.setVisibility(View.GONE);
                }
            });
    
    0 讨论(0)
提交回复
热议问题