将SVG文件从API加载到Kotlin中Android的ImageView中

bzzcjhmw  于 2022-11-16  发布在  Kotlin
关注(0)|答案(3)|浏览(222)

我在我的CryptoCurrencyTrackerApp中使用了Nomics API。我已经在github中搜索了我的问题,但我找不到合适的解决方案。我的应用程序工作完全好,但我不能加载加密货币标志在我的回收视图.这是一张照片,而我运行我的应用程序:

我已经设法把价格,名称,符号,但正如你所看到的,我不能加载图像API规范使用svg格式作为图像有人能帮助我加载imageviews这里是我的代码:

class CoinViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

private val nameView: TextView = itemView.findViewById(R.id.coinName)

private val priceView: TextView = itemView.findViewById(R.id.priceUsd)

private val currencyView: TextView = itemView.findViewById(R.id.coinSymbol)

private val imageCurrencyView : ImageView = itemView.findViewById(R.id.imgCurrencyIcon)

fun bind(cryptoModel: CoinModel) {

    this.nameView.text = cryptoModel.name

    this.currencyView.text = cryptoModel.currency
    
    when {

        cryptoModel.price ?: 0.0 > 100.0 -> {
            this.priceView.text = String.format("%.0f", cryptoModel.price)
        }

        cryptoModel.price ?: 0.0 > 1.0 -> {
            this.priceView.text = String.format("%.2f", cryptoModel.price)
        }

        else -> {
            this.priceView.text = String.format("%.4f", cryptoModel.price)
        }

    }

}

}我的货币片段

class CurrencyFragment : Fragment() {

private val baseUrl = "https://api.nomics.com/v1/currencies/"

private var cryptoModels: ArrayList<CoinModel>? = arrayListOf()

private var fragmentView: View? = null

private var recyclerView: RecyclerView? = null

private var coinAdapter: CoinAdapter? = null


override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {

    if (fragmentView == null) {
        fragmentView = inflater.inflate(R.layout.fragment_currency, container, false)
    }

    return fragmentView
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    recyclerView = view.findViewById(R.id.coin_recycler_view)

    coinAdapter = context?.let { CoinAdapter(it, arrayListOf()) }

    recyclerView?.layoutManager =
        LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)

    recyclerView?.adapter = coinAdapter

    loadData()

}

private fun loadData() {

    val retrofit = Retrofit.Builder()
        .baseUrl(baseUrl)
        .addConverterFactory(GsonConverterFactory.create())
        .build()

    val service = retrofit.create(CryptoAPI::class.java)

    val call = service.getData()

    call.enqueue(object : Callback<List<CoinModel>> {
        @SuppressLint("NotifyDataSetChanged")
        override fun onResponse(
            call: Call<List<CoinModel>>,
            response: Response<List<CoinModel>>
        ) {
            if (response.isSuccessful) {

                response.body()?.let {
                    cryptoModels = ArrayList(it)

                    coinAdapter?.cryptoList = cryptoModels ?: arrayListOf()

                    coinAdapter?.notifyDataSetChanged()

                }
            }

        }

        override fun onFailure(call: Call<List<CoinModel>>, t: Throwable) {

            t.printStackTrace()

        }

    })

}

我的硬币模型

data class CoinModel(

var currency: String?="",

var price: Double? = 0.0,

var name: String?="",

var logo_url: String? = "",

var rank: String = "",

)我的硬币适配器

class CoinAdapter(var context: Context, var cryptoList: ArrayList<CoinModel>):RecyclerView.Adapter<CoinViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CoinViewHolder {
    val view = LayoutInflater.from(parent.context).inflate(R.layout.currency_item,parent,false)
    return CoinViewHolder(view,)

}

override fun onBindViewHolder(holder: CoinViewHolder, position: Int) {

    holder.bind(cryptoList[position])



}

override fun getItemCount(): Int {
    return cryptoList.count()
}

}最后是我的界面:

interface CryptoAPI {

@GET("ticker?key=mykey")

fun getData():Call<List<CoinModel>>

}

rfbsl7qr

rfbsl7qr1#

我已经找到了一个在imageview中显示SVG的解决方案。我也遇到了同样的问题,然后我使用了下面的库。它的工作很好,我可以在android Imageview中显示来自API和本地文件的SVG图像
Sharp lib for SVG

cyej8jka

cyej8jka2#

使用具有内置加载器的Coil Svg,SVG解码器也可以与Compose UI配合使用

h7wcgrx3

h7wcgrx33#

Coil可用于此。请将以下依赖项添加到您的build.gradle(:app)

implementation("io.coil-kt:coil:1.2.0")
    implementation("io.coil-kt:coil-svg:1.2.0")

为此创建一个方法:

fun ImageView.loadUrl(url: String) {

        val imageLoader = ImageLoader.Builder(this.context)
            .componentRegistry { add(SvgDecoder(this@loadUrl.context)) }
            .build()

        val request = ImageRequest.Builder(this.context)
            //.crossfade(true)
            //.crossfade(500)
            .placeholder(R.drawable.ic_placeholder)
            .error(R.drawable.ic_placeholder)
            .data(url)
            .target(this)
            .build()

        imageLoader.enqueue(request)
    }

最后调用要加载图像的方法,在本例中是适配器的onBindViewHolder

yourImageView.loadUrl(cryptoList[position].image)

相关问题