美文网首页程序员半栈工程师
Android矢量图Vector适配5.0之前设备

Android矢量图Vector适配5.0之前设备

作者: tthwang | 来源:发表于2017-12-17 10:34 被阅读75次

怎么使用矢量图,这里就不细说了。

直说怎么适配TextView的CompoundDrawables,就是我们常用的drawableLeft、drawableTop。。。

直接使用矢量图,在4.4及以下设备运行,会报错:

android.view.InflateException: Binary XML file line #0: Error inflating class TextView

例如我有个矢量图ic_vector_enter_12dp.xml

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="10dp"
    android:height="10dp"
    android:viewportHeight="1024.0"
    android:viewportWidth="1024.0">
    <path
        android:fillColor="#B2B2B2"
        android:pathData="M307,18.3L779.2,489.8C791.2,501.9 791.2,521.5 779.2,533.6L306,1006C293.9,1018.1 274.3,1018.1 262.3,1006C250.3,993.9 250.3,974.3 262.3,962.4L691.8,533.6C703.9,521.5 703.9,501.9 691.8,489.8L263.4,62C251.3,49.9 251.3,30.4 263.4,18.4C275.3,6.3 294.9,6.3 307,18.3Z" />
</vector>

只要在新建一个xml,ic_vector_compat_enter_12dp.xml使用layer-list标签把矢量图包裹在里面

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/ic_vector_enter_10dp"
        android:gravity="center" />
</layer-list>

OK,大功告成。

虽然这样使用矢量图,为我们剩了不少麻烦,但性能肯定变差了,当然,就要看我们的使用场景了。需要频繁切换回收的场景,就尽量避免使用了,不然你会发现明显的卡顿。

相关文章

网友评论

    本文标题:Android矢量图Vector适配5.0之前设备

    本文链接:https://www.haomeiwen.com/subject/endywxtx.html