可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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 Picasso library for this. But I don't know how to use it with Glide library. I have idea that some resource ready function is there but I don't know how to use it. Can anyone help me?
Code for Picasso Library
Picasso.with(mcontext).load(imgLinkArray.get(position).mUrlLink) .into(imageView, new Callback() { @Override public void onSuccess() { progressBar.setVisibility(View.GONE); } @Override public void onError() { } }) ;
Now How Can I do this with Glide?
Glide.with(mcontext).load(imgLinkArray.get(position).mUrlLink) .into(imageView);
I am able to load image by this with Glide but how can I write progressBar.setVisibility(View.GONE);
somewhere in code if image get loaded?
回答1:
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() { @Override public boolean onException(Exception e, String model, Target target, boolean isFirstResource) { return false; } @Override public boolean onResourceReady(GlideDrawable resource, String model, Target 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.
回答2:
My answer was based on out-dated APIs. See here for the more up-to-date answer.
回答3:
In exception put a condition for show again the ProgressBar
Glide.with(context) .load(image_url) .listener(new RequestListener() { @Override public boolean onException(Exception e, String model, Target target, boolean isFirstResource) { if(e instanceof UnknownHostException) progressBar.setVisibility(View.VISIBLE); return false; } @Override public boolean onResourceReady(GlideDrawable resource, String model, Target target, boolean isFromMemoryCache, boolean isFirstResource) { progressBar.setVisibility(View.GONE); return false; } }) .into(imageView);
回答4:
If you want to do this in KOTLIN, you can try that way:
Glide.with(context) .load(url) .listener(object : RequestListener { override fun onLoadFailed(p0: GlideException?, p1: Any?, p2: Target?, p3: Boolean): Boolean { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } override fun onResourceReady(p0: Drawable?, p1: Any?, p2: Target?, p3: DataSource?, p4: Boolean): Boolean { Log.d(TAG, "OnResourceReady") //do something when picture already loaded return false } }) .into(imgView)
回答5:
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); } });
回答6:
- In xml take progress bar with height & width(match_parent).
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() { @Override public boolean onException(Exception e, String model, Target target, boolean isFirstResource) { progressBar.setVisibility(View.GONE); return false; } @Override public boolean onResourceReady(GlideDrawable resource, String model, Target target, boolean isFromMemoryCache, boolean isFirstResource) { progressBar.setVisibility(View.GONE); return false; } }) .into(imageView); }//setImageWIthProgressBar
回答7:
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.