当前位置: 首页 > 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)}
}

相关文章:

【Android】Glide加载SVG,SVG转PNG

Dependency plugins {id kotlin-kapt }dependencies {api com.github.bumptech.glide:glide:4.12.0kapt com.github.bumptech.glide:compiler:4.12.0api com.caverock:androidsvg:1.4 }SvgDecoder 负责解码SVG资源 import com.bumptech.glide.load.Options import com.bumpte…...

Spring、SpringMVC、Springboot三者的区别和联系

1.背景 最近有人问面试的一个问题&#xff1a;Spring、SpringMVC、Springboot三者的区别和联系&#xff0c;个人觉得&#xff1a;万变不离其宗&#xff0c;只需要理解其原理&#xff0c;回答问题信手拈来。 2.三者区别和联系 2.1 先了解Spring基础 Spring 框架就像一个家族…...

一点点安全资料:网络安全扩展

协议扩展 加密协议SSL/TLS 简介 SSL&#xff08;Secure Sockets Layer&#xff09;和TLS&#xff08;Transport Layer Security&#xff09;是加密协议&#xff0c;设计用来提供网络通信的安全性和数据完整性。尽管TLS是SSL的后继者&#xff0c;但两者的核心目标相同&#x…...

vscode的源码插件GitHub Repositories

打铁还需自身硬&#xff0c;需要不断提升自我&#xff0c;提升自我的一种方式就是看源码&#xff0c;站在更高的维度去理解底层原理&#xff0c;以便以后更好的开发和解决问题&#xff0c;由于源码一个动不动就是几个G甚至十几个G&#xff0c;如果一个个源码下载下来&#xff0…...

如何定义快速开发平台框架?有何突出优势?

作为提质增效的利器软件&#xff0c;快速开发平台框架如何能在众多同行中取胜&#xff1f;又是凭借什么优势特点在激烈的市场竞争中获得众多客户的青睐与信任&#xff1f;不管是从企业角度、服务商角度&#xff0c;还是使用者的角度来说&#xff0c;做好流程化进程&#xff0c;…...

二分练习题——奶牛晒衣服

奶牛晒衣服 题目分析 这里出现了“弄干所有衣服的最小时间”&#xff0c;那么可以考虑用二分去做。 第一阶段二段性分析 假设当前需要耗费的时间为mid分钟&#xff0c;如果mid分钟内可以烘干这些衣服&#xff0c;那么我们可以确定右边界大于mid的区间一定也可以。但是此时我…...

python工具包【1】 -- 不同操作系统路径转换

python工具包【1】 – 不同操作系统路径转换 以下的工具类的作用是根据不同的操作系统&#xff0c;将代码中的路径转换成适应操作系统的路径。 代码 import osclass Base_Tools_Cls:def BasePathConvert_func(self, path):根据不同的操作系统&#xff0c;将路径进行转换为不…...

JAVA中@FunctionalInterface 注解使用

FunctionalInterface是Java 8引入的一个注解&#xff0c;用于标记一个接口为函数式接口。函数式接口是指只有一个抽象方法&#xff08;除了Object类中的默认方法如equals、hashCode等&#xff09;的接口。在Java 8及以后版本中&#xff0c;函数式接口可以与lambda表达式配合使用…...

【Spring Cloud Alibaba】9 - OpenFeign集成Sentinel实现服务降级

目录 一、简介Sentinel 是什么如何引入Sentinel 二、服务搭建1.安装Sentinel控制台1.1 下载1.2 启动1.3 访问 2.改造服务提供者cloud-provider服务2.1 引入依赖2.2 添加API2.3 添加配置文件 3.改造cloud-consumer-feign服务3.1 引入依赖3.2 添加Feign接口3.3 添加服务降级类3.4…...

Chrome浏览器如何跟踪新开标签的网络请求?

在测试一个东西的时候&#xff0c;它虽然是a链接&#xff0c;但是&#xff0c;是由前端在js里写跳转的。我又必须要知道它的跳转链接&#xff0c;只能用截屏的方式来捕捉浏览器的地址栏链接 打开浏览器控制台(F12)点击红色箭头打钩为弹出式窗口自动打开DevTools 英文版调试参…...

html写一个登录注册页面

<!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>注册登录界面Ⅰ</title><link rel"stylesheet" href"https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.mi…...

Stable Diffusion|Ai赋能电商 Inpaint Anything

1. 背景介绍 随着人工智能技术的不断发展&#xff0c;其在电商领域的应用也越来越广泛。其中&#xff0c;图像修复技术在电商领域有着重要的应用价值。例如&#xff0c;在商品图片处理中&#xff0c;去除图片中的水印、瑕疵等&#xff0c;可以提高商品图片的质量和美观度。 2…...

启明智显M系列--工业级HMI芯片选型表

本章主要介绍启明智显M系列HMI主控芯片&#xff1a; 纯国产自主&#xff0c; RISC-V 内核&#xff0c;配备强大的 2D 图形加速处理器、PNG/JPEG 解码引擎、H.264解码&#xff1b;工业宽温&#xff0c;提供全开源SDK&#xff1b;1秒快速开机启动的特性&#xff0c;极大地提高了…...

C语言面试题之最小高度树

最小高度树 实例要求 1、给定一个有序整数数组&#xff0c;元素各不相同且按升序排列&#xff1b;2、编写一个算法&#xff0c;创建一棵高度最小的二叉搜索树&#xff1b;示例: 给定有序数组: [-10,-3,0,5,9],一个可能的答案是&#xff1a;[0,-3,9,-10,null,5]&#xff0c;它…...

【随笔】Git 高级篇 -- 整理提交记录(上)cherry-pick(十五)

&#x1f48c; 所属专栏&#xff1a;【Git】 &#x1f600; 作  者&#xff1a;我是夜阑的狗&#x1f436; &#x1f680; 个人简介&#xff1a;一个正在努力学技术的CV工程师&#xff0c;专注基础和实战分享 &#xff0c;欢迎咨询&#xff01; &#x1f496; 欢迎大…...

上门服务小程序|上门服务系统|上门服务软件开发流程

在如今快节奏的生活中&#xff0c;上门服务小程序的需求越来越多。它们向用户提供了方便、高效的服务方式&#xff0c;解决了传统服务行业中的很多痛点。如果你也想开发一个上门服务小程序&#xff0c;以下是开发流程和需要注意的事项。 1、确定需求&#xff1a;在开始开发之前…...

Vuex(vue 项目中实现 频繁、大范围数据共享的技术方案)

参考文档(点击查看) 好处 1.数据的存取一步到位&#xff0c;不需层层传递 2.数据的流动非常清晰 3.存储在Vuex中的数据都是响应式的&#xff08;数据更新后&#xff0c;使用数据的组件都会自动更新&#xff09; Vuex基础配置 npm i vuex3.6.2state中用来存储数据&#xff0c…...

【Spring Cloud】服务容错中间件Sentinel入门

文章目录 什么是 SentinelSentinel 具有以下特征&#xff1a;Sentinel分为两个部分: 安装 Sentinel 控制台下载jar包&#xff0c;解压到文件夹启动控制台访问了解控制台的使用原理 微服务集成 Sentinel添加依赖增加配置测试用例编写启动程序 实现接口限流总结 欢迎来到阿Q社区 …...

算法刷题记录 Day36

算法刷题记录 Day36 Date: 2024.04.02 lc 416. 分割等和子集 //2. 一维数组 class Solution { public:bool canPartition(vector<int>& nums) {// 将问题转化为从数组中任意取数&#xff0c;使得容量为数组总和一半的背包内的价值尽可能大。// dp[j]表示容积为j的…...

面试必问 - CSS 中元素居中小技巧

在网页设计中&#xff0c;居中是一个至关重要的布局技巧&#xff0c;能够确保你的页面在不同设备和屏幕尺寸上呈现出优雅的样式。 在这篇文章中&#xff0c;将介绍一些 CSS 居中的基本技巧&#xff0c;适用于各种场景。 1. 水平居中 文本水平居中 通过设置 text-align: cen…...

反向工程与模型迁移:打造未来商品详情API的可持续创新体系

在电商行业蓬勃发展的当下&#xff0c;商品详情API作为连接电商平台与开发者、商家及用户的关键纽带&#xff0c;其重要性日益凸显。传统商品详情API主要聚焦于商品基本信息&#xff08;如名称、价格、库存等&#xff09;的获取与展示&#xff0c;已难以满足市场对个性化、智能…...

测试markdown--肇兴

day1&#xff1a; 1、去程&#xff1a;7:04 --11:32高铁 高铁右转上售票大厅2楼&#xff0c;穿过候车厅下一楼&#xff0c;上大巴车 &#xffe5;10/人 **2、到达&#xff1a;**12点多到达寨子&#xff0c;买门票&#xff0c;美团/抖音&#xff1a;&#xffe5;78人 3、中饭&a…...

Cinnamon修改面板小工具图标

Cinnamon开始菜单-CSDN博客 设置模块都是做好的&#xff0c;比GNOME简单得多&#xff01; 在 applet.js 里增加 const Settings imports.ui.settings;this.settings new Settings.AppletSettings(this, HTYMenusonichy, instance_id); this.settings.bind(menu-icon, menu…...

华为OD机试-食堂供餐-二分法

import java.util.Arrays; import java.util.Scanner;public class DemoTest3 {public static void main(String[] args) {Scanner in new Scanner(System.in);// 注意 hasNext 和 hasNextLine 的区别while (in.hasNextLine()) { // 注意 while 处理多个 caseint a in.nextIn…...

【HTML-16】深入理解HTML中的块元素与行内元素

HTML元素根据其显示特性可以分为两大类&#xff1a;块元素(Block-level Elements)和行内元素(Inline Elements)。理解这两者的区别对于构建良好的网页布局至关重要。本文将全面解析这两种元素的特性、区别以及实际应用场景。 1. 块元素(Block-level Elements) 1.1 基本特性 …...

docker 部署发现spring.profiles.active 问题

报错&#xff1a; org.springframework.boot.context.config.InvalidConfigDataPropertyException: Property spring.profiles.active imported from location class path resource [application-test.yml] is invalid in a profile specific resource [origin: class path re…...

宇树科技,改名了!

提到国内具身智能和机器人领域的代表企业&#xff0c;那宇树科技&#xff08;Unitree&#xff09;必须名列其榜。 最近&#xff0c;宇树科技的一项新变动消息在业界引发了不少关注和讨论&#xff0c;即&#xff1a; 宇树向其合作伙伴发布了一封公司名称变更函称&#xff0c;因…...

tomcat入门

1 tomcat 是什么 apache开发的web服务器可以为java web程序提供运行环境tomcat是一款高效&#xff0c;稳定&#xff0c;易于使用的web服务器tomcathttp服务器Servlet服务器 2 tomcat 目录介绍 -bin #存放tomcat的脚本 -conf #存放tomcat的配置文件 ---catalina.policy #to…...

LangFlow技术架构分析

&#x1f527; LangFlow 的可视化技术栈 前端节点编辑器 底层框架&#xff1a;基于 &#xff08;一个现代化的 React 节点绘图库&#xff09; 功能&#xff1a; 拖拽式构建 LangGraph 状态机 实时连线定义节点依赖关系 可视化调试循环和分支逻辑 与 LangGraph 的深…...

HybridVLA——让单一LLM同时具备扩散和自回归动作预测能力:训练时既扩散也回归,但推理时则扩散

前言 如上一篇文章《dexcap升级版之DexWild》中的前言部分所说&#xff0c;在叠衣服的过程中&#xff0c;我会带着团队对比各种模型、方法、策略&#xff0c;毕竟针对各个场景始终寻找更优的解决方案&#xff0c;是我个人和我司「七月在线」的职责之一 且个人认为&#xff0c…...