当前位置: 首页 > news >正文

【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.InputStreamclass 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.SVGclass 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.FileOutputStreamclass HomeActivity : AppCompatActivity() {lateinit var binding: ActivityHomeBindingprivate 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.imageval listener = object : RequestListener<Drawable> {override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean): Boolean {return false}// write to png fileoverride 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)}
}
http://www.lryc.cn/news/334923.html

相关文章:

  • Spring、SpringMVC、Springboot三者的区别和联系
  • 一点点安全资料:网络安全扩展
  • vscode的源码插件GitHub Repositories
  • 如何定义快速开发平台框架?有何突出优势?
  • 二分练习题——奶牛晒衣服
  • python工具包【1】 -- 不同操作系统路径转换
  • JAVA中@FunctionalInterface 注解使用
  • 【Spring Cloud Alibaba】9 - OpenFeign集成Sentinel实现服务降级
  • Chrome浏览器如何跟踪新开标签的网络请求?
  • html写一个登录注册页面
  • Stable Diffusion|Ai赋能电商 Inpaint Anything
  • 启明智显M系列--工业级HMI芯片选型表
  • C语言面试题之最小高度树
  • 【随笔】Git 高级篇 -- 整理提交记录(上)cherry-pick(十五)
  • 上门服务小程序|上门服务系统|上门服务软件开发流程
  • Vuex(vue 项目中实现 频繁、大范围数据共享的技术方案)
  • 【Spring Cloud】服务容错中间件Sentinel入门
  • 算法刷题记录 Day36
  • 面试必问 - CSS 中元素居中小技巧
  • Chatgpt润色论文
  • 51单片机实验02- P0口流水灯实验
  • 使用git 和 github协作开发
  • DataX,MongoDB数据导入hdfs与mysql
  • 【OpenCV-颜色空间】
  • 电脑硬盘分区表的两种格式:MBR 和 GPT
  • kafka 常用非基础的核心设置项
  • 杂谈 EV之我见
  • 白色磨砂质感html5页源码
  • sqlite建立数据库
  • HTML5标签(网页编程)