篇首语:本文由编程笔记#小编为大家整理,主要介绍了Android 安装包优化Android 中使用 SVG 图片 ( 使用 appcompat 支持库兼容 5.0 以下版本的 Android 系统使用矢量图 )相关的知识,希望对你有一定的参考价值。
文章目录
- 一、使用 appcompat 支持库兼容 5.0 以下版本的 Android 系统使用矢量图
- 二、完整代码示例
- 1、build.gradle 构建脚本
- 2、布局文件
- 3、运行效果
- 三、参考资料
一、使用 appcompat 支持库兼容 5.0 以下版本的
android 系统使用矢量图
参考 Android 官方文档 : 添加多密度矢量图形
使用支持库添加对矢量图资源的支持 :
- com.android.support:appcompat-v7 支持库版本需要
23.2
23.2
23.2 以上 , 或使用 androidx.appcompat:appcompat 支持库 ; - Gradle 插件 , 版本需要
2.0
2.0
2.0 以上 ;
满足上述版本要求后 , 在 build.gradle 构建脚本的 " android / defaultConfig " 层级下 , 添加矢量图支持 , vectorDrawables.useSupportLibrary = true
;
在 dependencies 中添加支持库 : compile 'com.android.support:appcompat-v7:23.2.0'
或 implementation 'androidx.appcompat:appcompat:1.2.0'
二选一即可 ;
现在的应用创建后自带 implementation ‘androidx.appcompat:appcompat:1.2.0’ 支持 ;
构建脚本配置 :
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
}
引用矢量图 : 在布局文件中 , 使用 app:srcCompat 属性标签 , 设置矢量图 ;
<ImageView
android:layout_width&#61;"100dip"
android:layout_height&#61;"100dip"
app:layout_constraintBottom_toBottomOf&#61;"parent"
app:layout_constraintLeft_toLeftOf&#61;"parent"
app:layout_constraintRight_toRightOf&#61;"parent"
app:layout_constraintTop_toTopOf&#61;"parent"
app:srcCompat&#61;"&#64;drawable/ic_plane"/>
![在这里插入图片描述](https://img6.php1.cn/3cdc5/c45f/42f/2a25b3bbcbb3de6c.jpeg)
运行效果 :
![在这里插入图片描述](https://img6.php1.cn/3cdc5/c45f/42f/df0450794b3e9175.jpeg)
二、完整代码示例
1、build.gradle 构建脚本
plugins {
id &#39;com.android.application&#39;
id &#39;kotlin-android&#39;
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "kim.hsl.svg"
minSdkVersion 18
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary &#61; true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile(&#39;proguard-android-optimize.txt&#39;), &#39;proguard-rules.pro&#39;
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget &#61; &#39;1.8&#39;
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation &#39;androidx.core:core-ktx:1.3.2&#39;
implementation &#39;androidx.appcompat:appcompat:1.2.0&#39;
implementation &#39;com.google.android.material:material:1.3.0&#39;
implementation &#39;androidx.constraintlayout:constraintlayout:2.0.4&#39;
testImplementation &#39;junit:junit:4.&#43;&#39;
androidTestImplementation &#39;androidx.test.ext:junit:1.1.2&#39;
androidTestImplementation &#39;androidx.test.espresso:espresso-core:3.3.0&#39;
}
2、布局文件
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android&#61;"http://schemas.android.com/apk/res/android"
xmlns:app&#61;"http://schemas.android.com/apk/res-auto"
xmlns:tools&#61;"http://schemas.android.com/tools"
android:layout_width&#61;"match_parent"
android:layout_height&#61;"match_parent"
tools:context&#61;".MainActivity">
<ImageView
android:layout_width&#61;"100dip"
android:layout_height&#61;"100dip"
app:layout_constraintBottom_toBottomOf&#61;"parent"
app:layout_constraintLeft_toLeftOf&#61;"parent"
app:layout_constraintRight_toRightOf&#61;"parent"
app:layout_constraintTop_toTopOf&#61;"parent"
app:srcCompat&#61;"&#64;drawable/ic_plane"/>
androidx.constraintlayout.widget.ConstraintLayout>
3、运行效果
![在这里插入图片描述](https://img6.php1.cn/3cdc5/c45f/42f/df0450794b3e9175.jpeg)
三、参考资料
参考文档 :
-
添加多密度矢量图形 SVG : https://developer.android.google.cn/studio/write/vector-asset-studio
-
缩减、混淆处理和优化应用 : https://developer.android.google.cn/studio/build/shrink-code
-
SVG 语法格式 : https://www.runoob.com/svg/svg-tutorial.html
博客资源 :