怎么使用矢量图,这里就不细说了。
直说怎么适配 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,大功告成。
虽然这样使用矢量图,为我们剩了不少麻烦,但性能肯定变差了,当然,就要看我们的使用场景了。需要频繁切换回收的场景,就尽量避免使用了,不然你会发现明显的卡顿。
来源: http://www.jianshu.com/p/d23a33716bb9