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

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

直说怎么适配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,大功告成。

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。