Skip to content Skip to sidebar Skip to footer

How Do I Load Svg String Into Imageview

I have an SVG string Copy

Solution 2:

Accepted Answer does not really help me as Glide was throwing an exception asking to create a ModelLoader for type ByteArrayInputStream.Adding to the @Aweda's answer I used the AndroidSVG library as below.

Dependencies

//AndroidSVG
implementation 'com.caverock:androidsvg-aar:1.4'//Glide (add only if you're going to use Glide to load the  image)
implementation 'com.github.bumptech.glide:glide:4.12.0'
kapt 'com.github.bumptech.glide:compiler:4.12.0'

Code snippet

//SVG string content 
 val svgString ="<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 60 30\" width=\"1200\" height=\"600\"><clipPath id=\"a\"><path d=\"M30 15h30v15zv15h-30zh-30v-15zv-15h30z\"/></clipPath><path d=\"M0 0v30h60v-30z\" fill=\"#00247d\"/><path d=\"M0 0l60 30m0-30l-60 30\" stroke=\"#fff\" stroke-width=\"6\"/><path d=\"M0 0l60 30m0-30l-60 30\" clip-path=\"url(#a)\" stroke=\"#cf142b\" stroke-width=\"4\"/><path d=\"M30 0v30m-30-15h60\" stroke=\"#fff\" stroke-width=\"10\"/><path d=\"M30 0v30m-30-15h60\" stroke=\"#cf142b\" stroke-width=\"6\"/></svg>"//convert SVG string to an object of type SVG
val svg =SVG.getFromString(svgString)

//create a drawable from svg
val drawable =PictureDrawable(svg.renderToPicture())

//finally load the drawable with Glide.Glide.with(this)
            .load(drawable)
            .into(yourImageView)

In case you do not want to use Glide and want to set the SVG image to ImageView directly, do as follow:

yourImageView.setImageDrawable(drawable);

Post a Comment for "How Do I Load Svg String Into Imageview"