android Glide加载webp

1,813 阅读1分钟
  def GLIDE_VERSION = "4.13.2"
// webpdecoder
    implementation "com.github.zjupure:webpdecoder:2.3.${GLIDE_VERSION}"
// glide 4.10.0+
    implementation "com.github.bumptech.glide:glide:${GLIDE_VERSION}"
    annotationProcessor "com.github.bumptech.glide:compiler:${GLIDE_VERSION}"
val circleCrop: Transformation<Bitmap> = CenterCrop()

Glide.with(holder.itemView)
    .load("http://v.cdn.doupingit.com/source/0b97ab70-ac04-41a3-94e7-a684d811635d_small.webp")
    .optionalTransform(circleCrop)
    .optionalTransform(WebpDrawable::class.java, WebpDrawableTransformation(circleCrop))
    .addListener(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 {
            if(resource is WebpDrawable){
                resource.loopCount  = LOOP_FOREVER
            }
            return false
        }

    })
    .into(coverL)