项目中要使用SVG图片,SVG是个矢量图,效果真心赞啊,据说资源占用也少。而且程序猿后期可以修改颜色,因为转换后的SVG图片实质上是一个xml。
话不多说,Android项目中使用SVG图片这样来搞:
defaultConfig中加入:vectorDrawables.useSupportLibrary =
true
貌似还需要这俩库:
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:design:23.2.1'
加入依赖是因为Android从5.0以后才支持SVG,加入这些东西是为了兼容5.0以下版本。
defaultConfig {
applicationId "com.xxx.xxx"
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0.0"
vectorDrawables.useSupportLibrary =
true
buildConfigField "boolean", "ENABLE_ANALYTICS",
"true"
}
使用SVG图片步骤:转换-->布局文件中使用app:srcCompat-->成功
转换:Android Studio安装插件:SVG to VectorDrawable
SVG to VectorDrawable插件
点击"..."选择UI提供的SVG图,
选择UI提供的SVG图
点击Generate
点击Generate,成功转换
转换后的文件自动进入到drawable目录下
使用:布局中加入:xmlns:app="http://schemas.android.com/apk/res-auto"
设置图片时,如ImageView:
android:id="@+id/xxx_logo"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerInParent="true"
app:srcCompat="@drawable/图片路径"/>
设置资源文件时,只需这样设置:app:srcCompat="@drawable/图片路径"/
这样设置可以兼容5.0以下版本。
转换后的SVG图片实质上是一个XML。