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("http://zp-o.img.leiniao.com/live/d75a11e7-d2d4-4880-83b3-c358423f1393x.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中设置加载错误的图片

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值