Android 将图像从url加载到ImageView之前,将出现一个白色屏幕
如何解决这个问题?是否有其他组件比ImageView更好地用于显示来自服务器的图像? 目的:在我的应用程序中显示广告2秒钟。 当间隔设置为3秒时,图像加载工作正常。但我缩短到2秒,然后它会显示一个白色的屏幕,然后出现图像 来自url的Diply图像的主代码Android 将图像从url加载到ImageView之前,将出现一个白色屏幕,android,imageview,Android,Imageview,如何解决这个问题?是否有其他组件比ImageView更好地用于显示来自服务器的图像? 目的:在我的应用程序中显示广告2秒钟。 当间隔设置为3秒时,图像加载工作正常。但我缩短到2秒,然后它会显示一个白色的屏幕,然后出现图像 来自url的Diply图像的主代码 Glide.with(this) .load(imageUri) .diskCacheStrategy(DiskCacheStrategy.NONE)
Glide.with(this)
.load(imageUri)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.into(adImage);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
LoadToResumeActivity();
//adImage.setVisibility(View.GONE);
isAdEnabled = false;
resume_layout.setVisibility(View.VISIBLE);
}
}, 2000);
我认为您不需要延迟的
处理程序
,只需将延迟的代码放入Glide
的onResourceReady
。并不能保证每次加载图像都会在2秒内完成,所以最佳做法是,只有在加载图像时才可以看到视图
Java代码
Glide.with(context)
.load(imageUri)
.listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
return false; // important to return false so the error placeholder can be placed
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
return false;
}
})
.into(adImage);
Glide.with(上下文)
.load(图像URI)
.listener(新的RequestListener(){
@凌驾
public boolean onload失败(@Nullable glide,对象模型,目标,boolean isFirstResource){
return false;//返回false以放置错误占位符很重要
}
@凌驾
公共布尔onResourceReady(可绘制资源、对象模型、目标、数据源数据源、布尔isFirstResource){
返回false;
}
})
.进入(A图像);
科特林代码
Glide.with(this)
.load(imageUri)
.listener(object : RequestListener<Drawable> {
override fun onLoadFailed(e: GlideException?, model: Any?, target: com.bumptech.glide.request.target.Target<Drawable>?, isFirstResource: Boolean): Boolean {
return false
}
override fun onResourceReady(resource: Drawable?, model: Any?, target: com.bumptech.glide.request.target.Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
return false
}
})
.apply(RequestOptions()
.dontTransform()
.centerCrop()
.into(adImage)
Glide.with(这个)
.load(imageUri)
.listener(对象:RequestListener{
重写fun onLoadFailed(e:GlideException?,型号:Any?,目标:com.bumptech.glide.request.target.target?,isFirstResource:Boolean):布尔{
返回错误
}
覆盖fun onResourceReady(资源:Drawable?,模型:Any?,目标:com.bumptech.glide.request.target.target?,数据源:dataSource?,isFirstResource:Boolean):布尔{
返回错误
}
})
.apply(RequestOptions()
.dontTransform()
.centerCrop()
.into(adImage)
每当用户界面自行更新或网络调用时,在网络调用成功之前显示占位符或进度条的最佳方式。有关占位符的更多信息,请查看显示占位符以加载图像的最佳方式
Glide.with(this).load(photoToLoad.getPath()).placeholder(imageView.getDrawable()).into(imageView);
在上传图像之前显示错误或占位符图像当我们设置占位符图像时,它也会在显示准确图像之前显示一段时间?这可能会混淆用户权限?对于一般用途,占位符是最好的主意,但如果您希望在主图像加载之前使用缩略图,您可以在mage加载。谢谢你的回复。它的kotlin代码正确吗?你知道如何在java中实现吗?这是官方的,这是避免白色屏幕吗?