Does Glide have a method for loading both PNG and SVG?

后端 未结 7 1567
终归单人心
终归单人心 2020-11-29 06:34

I\'m using Glide to load some images asynchronously into some of my ImageViews, and I know it can handle images like PNG or

相关标签:
7条回答
  • 2020-11-29 07:25

    I added a flexible decoding pipeline to decode an image or SVG, maybe can help! based on glide SVG example

    Decoder

    class SvgOrImageDecoder : ResourceDecoder<InputStream, SvgOrImageDecodedResource> {
    
    override fun handles(source: InputStream, options: Options): Boolean {
        return true
    }
    
    @Throws(IOException::class)
    override fun decode(
        source: InputStream, width: Int, height: Int,
        options: Options
    ): Resource<SvgOrImageDecodedResource>? {
        val array = source.readBytes()
        val svgInputStream = ByteArrayInputStream(array.clone())
        val pngInputStream = ByteArrayInputStream(array.clone())
    
        return try {
            val svg = SVG.getFromInputStream(svgInputStream)
    
            try {
                source.close()
                pngInputStream.close()
            } catch (e: IOException) {}
    
            SimpleResource(SvgOrImageDecodedResource(svg))
        } catch (ex: SVGParseException) {
            try {
                val bitmap = BitmapFactory.decodeStream(pngInputStream)
                SimpleResource(SvgOrImageDecodedResource(bitmap = bitmap))
            } catch (exception: Exception){
                try {
                    source.close()
                    pngInputStream.close()
                } catch (e: IOException) {}
                throw IOException("Cannot load SVG or Image from stream", ex)
            }
        }
    }
    

    Transcoder

    class SvgOrImageDrawableTranscoder : ResourceTranscoder<SvgOrImageDecodedResource, PictureDrawable> {
    override fun transcode(
        toTranscode: Resource<SvgOrImageDecodedResource>,
        options: Options
    ): Resource<PictureDrawable>? {
        val data = toTranscode.get()
    
        if (data.svg != null) {
            val picture = data.svg.renderToPicture()
            val drawable = PictureDrawable(picture)
            return SimpleResource(drawable)
        } else if (data.bitmap != null)
            return SimpleResource(PictureDrawable(renderToPicture(data.bitmap)))
        else return null
    }
    
    private fun renderToPicture(bitmap: Bitmap): Picture{
        val picture = Picture()
        val canvas = picture.beginRecording(bitmap.width, bitmap.height)
        canvas.drawBitmap(bitmap, null, RectF(0f, 0f, bitmap.width.toFloat(), bitmap.height.toFloat()), null)
        picture.endRecording();
    
        return picture
    }
    

    Decoded Resource

    data class SvgOrImageDecodedResource(
    val svg:SVG? = null,
    val bitmap: Bitmap? = null)
    

    Glide Module

    class AppGlideModule : AppGlideModule() {
    override fun registerComponents(
        context: Context, glide: Glide, registry: Registry
    ) {
        registry.register(SvgOrImageDecodedResource::class.java, PictureDrawable::class.java, SvgOrImageDrawableTranscoder())
            .append(InputStream::class.java, SvgOrImageDecodedResource::class.java, SvgOrImageDecoder())
    }
    
    // Disable manifest parsing to avoid adding similar modules twice.
    override fun isManifestParsingEnabled(): Boolean {
        return false
    }
    

    }

    0 讨论(0)
提交回复
热议问题