【发布时间】:2018-12-24 15:47:08
【问题描述】:
我正在构建一个需要从服务器下载 svg 图像的 android 应用。 我已经尝试以通常的方式使用毕加索,但它什么也没显示。
Picasso.get().load(url).into(imageView)
有没有用毕加索显示矢量图像?
【问题讨论】:
-
Here 是关于它的答案。
我正在构建一个需要从服务器下载 svg 图像的 android 应用。 我已经尝试以通常的方式使用毕加索,但它什么也没显示。
Picasso.get().load(url).into(imageView)
有没有用毕加索显示矢量图像?
【问题讨论】:
您可以使用名为 GlideToVectorYou 的库,它在内部使用 Glide。
fun ImageView.loadSvg(url: String?) {
GlideToVectorYou
.init()
.with(this.context)
.setPlaceHolder(R.drawable.loading, R.drawable.actual)
.load(Uri.parse(url), this)
}
或者您也可以使用 Coil 库来加载 svg。只需在 build.gradle 中添加这些行
// ... Coil (https://github.com/coil-kt/coil)
implementation("io.coil-kt:coil:0.12.0")
implementation("io.coil-kt:coil-svg:0.12.0")
然后添加一个扩展函数
fun AppCompatImageView.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)
.data(url)
.target(this)
.build()
imageLoader.enqueue(request)
}
然后在你的activity或者fragment中调用这个方法
your_image_view.loadSvg("your_file_name.svg")
【讨论】:
我的建议,尝试转移到 Glide 库。在引擎盖下,lib 可以加载 svg 和更多的东西。 Here is an examples.
【讨论】:
在 kotlin 中使用线圈处理 svg 文件
先添加
// Coil
implementation "io.coil-kt:coil-compose:1.3.2"
implementation "io.coil-kt:coil-svg:1.3.2"
然后使用
fun ImageView.loadImage(imageUri: String, placeholder: Int? = null) {
val imageLoader = ImageLoader.Builder(this.context)
.componentRegistry { add(SvgDecoder(this@loadImage.context)) }
.build()
load(uri = imageUri, imageLoader = imageLoader) {
crossfade(true)
placeholder(placeholder ?: R.drawable.image_avatar)
}
}
【讨论】: