Easiest way to use SVG in Android?

前端 未结 8 966
一个人的身影
一个人的身影 2020-12-02 05:35

I have found a myriad of libraries in order to use svg in Android and avoid the frustrating creation of different resolutions and dropping files for each resolution, this be

相关标签:
8条回答
  • 2020-12-02 05:55

    You can 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")
    

    Then Add an extension function

    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")
    
    0 讨论(0)
  • 2020-12-02 06:03

    Android Studio supports SVG from 1.4 onwards

    Here is a video on how to import.

    0 讨论(0)
提交回复
热议问题