Glide加载图片占位图问题,CustomViewTarget加载图片占位图问题

编程入门 行业动态 更新时间:2024-10-25 12:25:07

Glide加载图片占<a href=https://www.elefans.com/category/jswz/34/1763618.html style=位图问题,CustomViewTarget加载图片占位图问题"/>

Glide加载图片占位图问题,CustomViewTarget加载图片占位图问题

Glide加载图片时通常会设置占位图。

1.Glide加载图片到imageview设置占位图

   val options = RequestOptions().placeholder(R.drawable.tlive_main_img_poster_default)//图片加载出来前,显示的图片.fallback( R.drawable.tlive_main_img_poster_default) //url为空的时候,显示的图片.error(R.drawable.tlive_main_img_poster_default);//图片加载失败后,显示的图片Glide.with(this).load(".png").apply(options).into(imageposter)

加载图片到imageview的直接使用RequestOptions即可

2.Glide以bitmap形式加载图片设置占位图

fun loadImageIntoBackground(view: View,context: Context,url: String,corner: Int,placeHolder: Int,width: Int,height: Int,imageLoaderDrawableListener: ImageLoaderDrawableListener) {val option: RequestOptions = RequestOptions().override(width, height).transform(RoundedCorners(corner)).placeholder(placeHolder).error(placeHolder).fallback(placeHolder)Glide.with(context).asBitmap().load(url).apply(option).into(object : CustomViewTarget<View, Bitmap>(view) {override fun onLoadFailed(errorDrawable: Drawable?) {LogUtils.d("ImageLoadUtils", "onLoadFailed${errorDrawable}")imageLoaderDrawableListener.onLoadFail(errorDrawable)}override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {if (resource != null && !resource.isRecycled) {imageLoaderDrawableListener.onLoadImageSuccess(url,BitmapDrawable(context.resources, resource))}}override fun onResourceLoading(placeholder: Drawable?) {LogUtils.d("ImageLoadUtils", "onResourceLoading${placeholder}")super.onResourceLoading(placeholder)view.background = placeholder}override fun onResourceCleared(placeholder: Drawable?) {}})}

使用CustomViewTarget方式加载图片需要在onResourceLoading回调中设置占位图,onLoadFailed中设置加载错误的图片

更多推荐

Glide加载图片占位图问题,CustomViewTarget加载图片占位图问题

本文发布于:2023-11-15 19:52:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1605359.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:位图   加载   图片   Glide   CustomViewTarget

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!