从 ImageView 上的 url 加载 svg 文件
Load svg file from url on ImageView
我有一个 this 类型的 URL Jira REST API
,URL 末尾没有 SVG
扩展名或文件名。
在浏览器中打开时,它会显示图像。
我想在 ImageView
中显示图像,但这似乎不是来自服务器的文件路径。
我研究过如何将 SVG 从 drawables
here 显示到 ImageView
,但没有研究从这些类型的 URL 加载。
我正在使用 Volley
(如果重要的话)。
当我检查 Chrome 中的元素并看到 Network > Response tab
时,我可以看到 Content-Type:image/svg+xml;charset=UTF-8
。
如何在 ImageView 中使用这种类型的 URL 来显示图像?
ImageView 只接受位图或 VectorDrawable。
SVG 不是两者中的任何一种,即使 VectorDrawable 是它的后代。
如果你不想使用外部库将 SVG 转换为位图,那么我可以建议你使用 WebView 来显示它
或者,您可能希望使用免费的在线工具将 SVG 转换为 VectorDrawable。
kotlin + 线圈
添加依赖:
//Coil (https://github.com/coil-kt/coil)
implementation("io.coil-kt:coil:1.2.0")
implementation("io.coil-kt:coil-svg:1.2.0")
将此函数添加到您的代码中:
fun ImageView.loadSvg(url: String) {
val imageLoader = ImageLoader.Builder(this.context)
.componentRegistry { add(SvgDecoder(this@loadSvg.context)) }
.build()
val request = ImageRequest.Builder(this.context)
.crossfade(true)
.crossfade(500)
.placeholder(R.drawable.placeholder)
.error(R.drawable.error)
.data(url)
.target(this)
.build()
imageLoader.enqueue(request)
}
然后在您的 activity 或片段中调用此方法:
imageView.loadSvg("your_file_name.svg")
// OR imageView.loadSvg(url)
我有一个 this 类型的 URL Jira REST API
,URL 末尾没有 SVG
扩展名或文件名。
在浏览器中打开时,它会显示图像。
我想在 ImageView
中显示图像,但这似乎不是来自服务器的文件路径。
我研究过如何将 SVG 从 drawables
here 显示到 ImageView
,但没有研究从这些类型的 URL 加载。
我正在使用 Volley
(如果重要的话)。
当我检查 Chrome 中的元素并看到 Network > Response tab
时,我可以看到 Content-Type:image/svg+xml;charset=UTF-8
。
如何在 ImageView 中使用这种类型的 URL 来显示图像?
ImageView 只接受位图或 VectorDrawable。
SVG 不是两者中的任何一种,即使 VectorDrawable 是它的后代。
如果你不想使用外部库将 SVG 转换为位图,那么我可以建议你使用 WebView 来显示它
或者,您可能希望使用免费的在线工具将 SVG 转换为 VectorDrawable。
kotlin + 线圈
添加依赖:
//Coil (https://github.com/coil-kt/coil)
implementation("io.coil-kt:coil:1.2.0")
implementation("io.coil-kt:coil-svg:1.2.0")
将此函数添加到您的代码中:
fun ImageView.loadSvg(url: String) {
val imageLoader = ImageLoader.Builder(this.context)
.componentRegistry { add(SvgDecoder(this@loadSvg.context)) }
.build()
val request = ImageRequest.Builder(this.context)
.crossfade(true)
.crossfade(500)
.placeholder(R.drawable.placeholder)
.error(R.drawable.error)
.data(url)
.target(this)
.build()
imageLoader.enqueue(request)
}
然后在您的 activity 或片段中调用此方法:
imageView.loadSvg("your_file_name.svg")
// OR imageView.loadSvg(url)