Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use SVG image in ImageView

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.

like image 539
VLeonovs Avatar asked Jan 25 '16 10:01

VLeonovs


People also ask

Can we use SVG image in Android Studio?

The Google Material Design specification provides material icons that you can use in your Android apps. Vector Asset Studio helps you choose, import, and size material icons, as well as define opacity and the Right-to-Left (RTL) mirroring setting. Vector Asset Studio also lets you import your own SVG and PSD files.

Does glide support SVG?

The SVG sample app demonstrates how you can use Glide's flexible decoding pipeline to decode custom resource types. The SVG app loads SVG data from resources and over the network, and uses a custom decoder and drawable to display the SVG with Glide.

Is SVG supported in iOS?

SVGs will be supported on devices running iOS 13 and up. On devices below iOS 13, it appears SVGs will be converted to PNGs since they're not a supported format. SVGs are typically the preferred asset type for Web and Android platforms.


1 Answers

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" 
like image 95
Marek Multarzyński Avatar answered Sep 23 '22 02:09

Marek Multarzyński