如何使用Picasso库加载远程SVG文件

6

我正在开发一个需要从服务器下载svg图片的安卓应用程序。我尝试了通常的方式使用Picasso,但是它没有显示任何内容。

Picasso.get().load(url).into(imageView)

有没有办法使用Picasso显示矢量图像?

这里有关于它的答案:https://dev59.com/N14c5IYBdhLWcg3wMnz1#30938082 - Jurij Pitulja
3个回答

5
你可以使用一个叫做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)
}

然后在您的活动或片段中调用此方法。
your_image_view.loadSvg("your_file_name.svg")

2
根据问题,这是关于Picasso库而不是任何可能的方法,所以我认为你的答案与主题无关。有时候,为了一个单一的使用情况,没有办法改变主要的图像加载库,我们绝对不应该再添加另一个库! - Choletski

0

使用 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)
       }
   }

这是组合代码吗?为什么你要附加coil compose的依赖项? - Narek Hayrapetyan

-1

我的建议是尝试使用Glide库。在底层,该库可以加载svg和许多其他东西。 这里有一个例子


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接