【Android】Glide加载SVG,SVG转PNG

Dependency
plugins {
    id 'kotlin-kapt'
}

dependencies {
    api 'com.github.bumptech.glide:glide:4.12.0'
    kapt 'com.github.bumptech.glide:compiler:4.12.0'
    api 'com.caverock:androidsvg:1.4'
}
SvgDecoder

负责解码SVG资源


import com.bumptech.glide.load.Options
import com.bumptech.glide.load.ResourceDecoder
import com.bumptech.glide.load.engine.Resource
import com.bumptech.glide.load.resource.SimpleResource
import com.bumptech.glide.request.target.Target
import com.caverock.androidsvg.SVG
import java.io.InputStream

class SvgDecoder : ResourceDecoder<InputStream, SVG> {

    override fun handles(source: InputStream, options: Options): Boolean {
        return true
    }

    override fun decode(
        source: InputStream, width: Int, height: Int, options: Options
    ): Resource<SVG> {
        val svg = SVG.getFromInputStream(source)
        if (width != Target.SIZE_ORIGINAL)
            svg.documentWidth = width.toFloat()
        if (height != Target.SIZE_ORIGINAL)
            svg.documentHeight = height.toFloat()
        return SimpleResource(svg)
    }
}

SvgTranscoder

负责将SVG转为Android的Drawable或Bitmap


import android.graphics.drawable.PictureDrawable
import com.bumptech.glide.load.Options
import com.bumptech.glide.load.engine.Resource
import com.bumptech.glide.load.resource.SimpleResource
import com.bumptech.glide.load.resource.transcode.ResourceTranscoder
import com.caverock.androidsvg.SVG

class SvgTranscoder : ResourceTranscoder<SVG, PictureDrawable> {

    override fun transcode(
        toTranscode: Resource<SVG>, options: Options
    ): Resource<PictureDrawable> {
        val svg = toTranscode.get()
        val picture = svg.renderToPicture()
        val drawable = PictureDrawable(picture)
        return SimpleResource(drawable)
    }
}

SvgModule

注册Glide自定义插件


import android.content.Context
import android.graphics.drawable.PictureDrawable
import com.bumptech.glide.Glide
import com.bumptech.glide.Registry
import com.bumptech.glide.annotation.GlideModule
import com.bumptech.glide.module.AppGlideModule
import com.caverock.androidsvg.SVG
import java.io.InputStream

@GlideModule
class SvgModule : AppGlideModule() {

    override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
        registry.register(SVG::class.java, PictureDrawable::class.java, SvgTranscoder())
        registry.append(InputStream::class.java, SVG::class.java, SvgDecoder())
    }

    override fun isManifestParsingEnabled(): Boolean {
        return false
    }
}

GlideApp

编译会生成一个GlideApp,用它来取代默认的Glide加载资源即可


import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.drawable.Drawable
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import com.onyx.android.test.databinding.ActivityHomeBinding
import java.io.FileOutputStream

class HomeActivity : AppCompatActivity() {

    lateinit var binding: ActivityHomeBinding

    private val svg = "http://xxxxxxxxxxxxx.svg"
    private val png = "https://xxxxxxxxxxxxx.png"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityHomeBinding.inflate(layoutInflater)
        setContentView(binding.root)
    }

    override fun onResume() {
        super.onResume()
        test()
    }

    private fun test() {
        val image = binding.image
        val listener = object : RequestListener<Drawable> {

            override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean): Boolean {
                return false
            }

            // write to png file
            override fun onResourceReady(drawable: Drawable, model: Any?, target: Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
                val bitmap = Bitmap.createBitmap(
                    drawable.intrinsicWidth,
                    drawable.intrinsicHeight, Bitmap.Config.ARGB_8888
                )
                val canvas = Canvas(bitmap)
                drawable.setBounds(0, 0, canvas.width, canvas.height)
                drawable.draw(canvas)
                val file = "sdcard/0.png"
                FileOutputStream(file).use {
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, it)
                }
                return false
            }
        }
        GlideApp.with(this)
            .asDrawable()
            .load(svg)
            .addListener(listener)
            .into(image)
    }
}

相关推荐

  1. 【Android】GlideSVG,SVGPNG

    2024-04-08 18:28:03       38 阅读
  2. UE5_本地图片(jpg, png) UTexture

    2024-04-08 18:28:03       29 阅读
  3. 页面 跳 新页面 vue

    2024-04-08 18:28:03       41 阅读
  4. 苹果HEIC 数据 PNG

    2024-04-08 18:28:03       25 阅读
  5. pdfpng的两种方法

    2024-04-08 18:28:03       57 阅读
  6. js实现webppng/jpg

    2024-04-08 18:28:03       37 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-08 18:28:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-08 18:28:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-08 18:28:03       87 阅读
  4. Python语言-面向对象

    2024-04-08 18:28:03       96 阅读

热门阅读

  1. Linux学习第六天(进程)

    2024-04-08 18:28:03       26 阅读
  2. 算法| ss 二分

    2024-04-08 18:28:03       36 阅读
  3. Linux基本命令入门详解

    2024-04-08 18:28:03       33 阅读
  4. ubunut 18.04安装gcc-9 g++-9

    2024-04-08 18:28:03       29 阅读
  5. AI技术创业机会之金融科技

    2024-04-08 18:28:03       29 阅读
  6. MQ5之CCI交叉信号

    2024-04-08 18:28:03       32 阅读