Android 使用 Glide 库设置完成图像加载后进度条的可见性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26054420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set visibility of progress bar gone on completion of image loading using Glide library
提问by HariRam
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?
嗨,我想要一个图像进度条,它将在图像加载时显示,但是当图像加载完成时,我想将其设置为消失。早些时候我为此使用了毕加索库。但我不知道如何将它与 Glide 库一起使用。我知道那里有一些资源就绪功能,但我不知道如何使用它。谁能帮我?
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 做到这一点?
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?
我可以通过 Glide 加载图像,但是如果加载了图像,我该如何progressBar.setVisibility(View.GONE);
在代码中写入?
回答by Yaroslav
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.
如果想要自己处理动画之类的事情,则返回 true,如果想要滑行为您处理它们,则返回 false。
回答by Paulina
If you want to do this in KOTLIN, you can try that way:
如果您想在 KOTLIN 中执行此操作,可以尝试以下方式:
Glide.with(context)
.load(url)
.listener(object : RequestListener<Drawable> {
override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean): Boolean {
//TODO: something on exception
}
override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
Log.d(TAG, "OnResourceReady")
//do something when picture already loaded
return false
}
})
.into(imgView)
回答by Bryan Sills
回答by Alex Zaraos
In exception put a condition for show again the ProgressBar
在异常情况下再次显示 ProgressBar
Glide.with(context)
.load(image_url)
.listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
if(e instanceof UnknownHostException)
progressBar.setVisibility(View.VISIBLE);
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);
回答by Pratap
The above solution works pretty well for me too but when i use asBitmap()to download the image. It does not work.
上面的解决方案对我也很有效,但是当我使用asBitmap()下载图像时。这是行不通的。
We need to use BitmapImageViewTarget
我们需要使用 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);
}
});
回答by Alex
GlideDrawable are deprecated, use simple Drawable
不推荐使用 GlideDrawable,使用简单的 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);
回答by Android Dev
In Kotlin you can do like below
在 Kotlin 中,您可以执行以下操作
Glide.with(context)
.setDefaultRequestOptions(RequestOptions().placeholder(R.drawable.ic_image_placeholder).error(R.drawable.ic_image_placeholder))
.load(url)
.listener(object : RequestListener<Drawable>{
override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean): Boolean {
return false
}
override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
return false
}
})
.into(imageView)
回答by Narek Hayrapetyan
Update:
更新:
Glide.with(this)
.load(imageUrl)
.listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable final GlideException e,
final Object model, final Target<Drawable> target,
final boolean isFirstResource) {
showProgress(false);
mNoContentTextView.setVisibility(View.VISIBLE);
return false;
}
@Override
public boolean onResourceReady(final Drawable resource,
final Object model,
final Target<Drawable> target,
final DataSource dataSource,
final boolean isFirstResource) {
showProgress(false);
mNoContentTextView.setVisibility(View.GONE);
mContentImageView.setImageDrawable(resource);
return false;
}
})
.into(mContentImageView);
回答by Vijesh Jat
- 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<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
- 在 xml 中获取具有高度和宽度(match_parent)的进度条。
在调用下面提到的方法之前,设置进度条可见性可见。
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
回答by Dennis Gonzales
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.OnCompleted { }
给 Glide 的.listener()
GlideImpl.kt class that accepts Glide's RequestListener
接受 Glide 的 RequestListener 的 GlideImpl.kt 类
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!
就是这样!