美好的一天,我有一张SVG
图像。如何将其添加到ImageView
背景?
我尝试使用this library但我有问题:
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)
我做了什么?
首先,我将dependency
添加到gradle
:
compile 'com.caverock:androidsvg:1.2.1'
其次,我将ImageView
更改为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"
/>
之后我将xmls:svgimageview
添加到根布局:
<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">
此clock.svg
位于assets
文件夹中。
答案 0 :(得分:79)
在新的Android Studio中,可以将SVG导入XML文件,因此您无需使用外部库。
在可绘制的右键单击中 - &gt;新 - &gt;矢量资产 - &gt;本地SVG文件。
然后您通常将其用作其他drawable:
android:src="@drawable/btn_image"
答案 1 :(得分:2)
在默认情况下打开硬件渲染的较新设备上,您需要明确启用软件渲染。
imgView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
OR 在xml中添加此标记
android:layerType="software"
答案 2 :(得分:2)
要正确使用svgimageview:svg
属性,应用需要知道它的定义。不幸的是,目前只有一个JAR版本的库,并且(与AAR不同)所需的定义在构建应用程序时无法正确合并。
尝试从库源
复制attrs.xml
文件
https://github.com/BigBadaboom/androidsvg/blob/master/androidsvg/src/main/res/values/attrs.xml
进入res/values
文件夹。
我认为应该解决你的问题。
答案 3 :(得分:1)
尽管SVG是基于XML的矢量图像,但是Imageview只能渲染XML(矢量可绘制对象)。因此,我们需要将SVG转换为XML格式。我们可以使用Android Vector Asset Studio来做到这一点。
这可能有助于将SVG,PSD转换为XML
https://medium.com/@iamitgupta1994/converting-svg-psd-to-xml-vector-drawable-using-android-vector-asset-studio-8e8ec23d5405
答案 4 :(得分:0)
答案 5 :(得分:0)
找到svg图像的位置。右键单击res上的drawable文件夹, 选择new >>>> Vector Asset >>> Local SVG >>>>并将其另存为xml
答案 6 :(得分:-2)
ImageView只接受Bitmaps或VectorDrawables,它不能接受svg。一个简单的解决方案是添加一个webview,它将加载svg文件。在我收到数据pngs或svg的情况下。在布局中,我添加了一个可视性消失的webview,如果从服务器收到的数据是svg,我将它加载到webview中。