我正在开发一个需要从服务器下载svg图片的安卓应用程序。我尝试了通常的方式使用Picasso,但是它没有显示任何内容。
Picasso.get().load(url).into(imageView)
有没有办法使用Picasso显示矢量图像?
fun ImageView.loadSvg(url: String?) {
GlideToVectorYou
.init()
.with(this.context)
.setPlaceHolder(R.drawable.loading, R.drawable.actual)
.load(Uri.parse(url), this)
}
// ... 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)
}
your_image_view.loadSvg("your_file_name.svg")
使用 Kotlin 中的 Coil 处理 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)
}
}