如何使用 Picasso 库加载远程 svg 文件 [英] How to load remote svg files with Picasso library

查看:203
本文介绍了如何使用 Picasso 库加载远程 svg 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个需要从服务器下载 svg 图像的 android 应用程序.我曾尝试以通常的方式使用毕加索,但它什么也没显示.

I'm building an android app that requires downloading svg images from a server. I have tried using Picasso the usual way but it displays nothing.

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

无论如何可以用毕加索显示矢量图像吗?

Is there anyway to display vector images with Picasso?

推荐答案

您可以使用名为 GlideToVectorYou 内部使用 Glide.

You can use a library called GlideToVectorYou which uses Glide internally.

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 中添加这些行

or You can also use Coil library to load svg. Just add these lines in 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)
}

然后在您的活动或片段中调用此方法

Then call this method in your activity or fragment

your_image_view.loadSvg("your_file_name.svg")

这篇关于如何使用 Picasso 库加载远程 svg 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆