How to use SVG image in ImageView

VLeonovs picture VLeonovs · Jan 25, 2016 · Viewed 74k times · Source

Good Day, I have a SVG image. How can I add it to ImageView background ?

I tried to use this library But I have problem:

01-25 12:19:02.669 27719-27719/com.dvor.androidapp E/AndroidRuntime: FATAL EXCEPTION: main
    android.view.InflateException: Binary XML file line #70: Error inflating class com.caverock.androidsvg.SVGImageView
    at android.view.LayoutInflater.createView(LayoutInflater.java:626)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
    at com.dvor.mobileapp.checkout.ShoppingCart.onCreateView(ShoppingCart.java:411)
    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:953)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
   at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
   at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
   at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:456)
   at android.os.Handler.handleCallback(Handler.java:730)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:176)
   at android.app.ActivityThread.main(ActivityThread.java:5419)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:525)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
   at dalvik.system.NativeStart.main(Native Method)
                                                                          Caused by: java.lang.reflect.InvocationTargetException
   at java.lang.reflect.Constructor.constructNative(Native Method)
   at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
   at android.view.LayoutInflater.createView(LayoutInflater.java:600)
   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702) 
   at android.view.LayoutInflater.rInflate(LayoutInflater.java:761) 
   at android.view.LayoutInflater.rInflate(LayoutInflater.java:769) 
   at android.view.LayoutInflater.rInflate(LayoutInflater.java:769) 
   at android.view.LayoutInflater.rInflate(LayoutInflater.java:769) 
   at android.view.LayoutInflater.rInflate(LayoutInflater.java:769) 
   at android.view.LayoutInflater.rInflate(LayoutInflater.java:769) 
  at android.view.LayoutInflater.rInflate(LayoutInflater.java:769) 
  at android.view.LayoutInflater.inflate(LayoutInflater.java:498) 
  at android.view.LayoutInflater.inflate(LayoutInflater.java:398) 
  at com.dvor.mobileapp.checkout.ShoppingCart.onCreateView(ShoppingCart.java:411) 
  at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786) 
  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:953) 
  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136) 
 at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739) 
 at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499) 
  at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:456) 
  at android.os.Handler.handleCallback(Handler.java:730) 
  at android.os.Handler.dispatchMessage(Handler.java:92) 
  at android.os.Looper.loop(Looper.java:176) 
  at android.app.ActivityThread.main(ActivityThread.java:5419) 
  at java.lang.reflect.Method.invokeNative(Native Method) 
  at java.lang.reflect.Method.invoke(Method.java:525) 
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046) 
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862) 
  at dalvik.system.NativeStart.main(Native Method) 
  Caused by: java.lang.NoClassDefFoundError: com.caverock.androidsvg.R$styleable
  at com.caverock.androidsvg.SVGImageView.init(SVGImageView.java:80)
  at com.caverock.androidsvg.SVGImageView.<init>(SVGImageView.java:66)
  at java.lang.reflect.Constructor.constructNative(Native Method) 
  at java.lang.reflect.Constructor.newInstance(Constructor.java:417) 
  at android.view.LayoutInflater.createView(LayoutInflater.java:600) 
  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702) 
  at android.view.LayoutInflater.rInflate(LayoutInflater.java:761) 
  at android.view.LayoutInflater.rInflate(LayoutInflater.java:769) 
  at android.view.LayoutInflater.rInflate(LayoutInflater.java:769) 
  at android.view.LayoutInflater.rInflate(LayoutInflater.java:769) 
  at android.view.LayoutInflater.rInflate(LayoutInflater.java:769) 
  at android.view.LayoutInflater.rInflate(LayoutInflater.java:769) 
  at android.view.LayoutInflater.rInflate(LayoutInflater.java:769) 
  at android.view.LayoutInflater.inflate(LayoutInflater.java:498) 
  at android.view.LayoutInflater.inflate(LayoutInflater.java:398) 
  at com.dvor.mobileapp.checkout.ShoppingCart.onCreateView(ShoppingCart.java:411) 
  at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786) 
  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:953) 
  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136) 
 at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739) 
  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499) 
   at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:456) 
   at android.os.Handler.handleCallback(Handler.java:730) 
   at android.os.Handler.dispatchMessage(Handler.java:92) 
   at android.os.Looper.loop(Looper.java:176) 
   at android.app.ActivityThread.main(ActivityThread.java:5419) 
   at java.lang.reflect.Method.invokeNative(Native Method) 
   at java.lang.reflect.Method.invoke(Method.java:525) 
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862) 
   at dalvik.system.NativeStart.main(Native Method)

 

What I did ?

Firstly, I added dependency to gradle:

compile 'com.caverock:androidsvg:1.2.1'

Secondly, I changed ImageView to com.caverock.androidsvg.SVGImageView

 <com.caverock.androidsvg.SVGImageView
         android:id="@+id/recentlyViewed_imgView"
         android:layout_width="100dp"
         android:layout_height="100dp"
         svgimageview:svg="clock.svg"
         />

After that I added xmls:svgimageview to root layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:svgimageview="http://schemas.android.com/apk/res-auto"
    android:id="@+id/rowItem"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#272727"
    android:clickable="true"
    android:orientation="vertical">

This clock.svg is in the assets folder.

Answer

Marek Multarzyński picture Marek Multarzyński · Jan 25, 2016

In new Android Studio there is possibility to import SVG to XML file, so You don't need to use external library.

In drawable right click -> New -> Vector Asset -> Local SVG file.

enter image description here

Then You use it normally as other drawables:

android:src="@drawable/btn_image"