【Android 安装包优化】Android 中使用 SVG 图片 ( 使用 appcompat 支持库兼容 5.0 以下版本的 Android 系统使用矢量图 )

简介: 【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.223.2 以上 , 或使用 androidx.appcompat:appcompat 支持库 ;

Gradle 插件 , 版本需要 2.0 2.02.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 'com.android.support:appcompat-v7:23.2.0'
  implementation 'androidx.appcompat:appcompat:1.2.0'
}



引用矢量图 : 在布局文件中 , 使用 app:srcCompat 属性标签 , 设置矢量图 ;


 

<ImageView
        android:layout_width="100dip"
        android:layout_height="100dip"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_plane"/>




运行效果 :

image.png







二、完整代码示例




1、build.gradle 构建脚本


plugins {
    id 'com.android.application'
    id 'kotlin-android'
}
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"
        // 生成 PNG 图片配置
        //generatedDensities = ['hdpi', 'mdpi', 'xhdpi',  'xxhdpi', 'xxxhdpi']
        // 使用 com.android.support:appcompat 支持库配置
        vectorDrawables.useSupportLibrary = true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}
dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.3.2'
    // 矢量图支持库 , 支持 5.0 以下版本手机使用矢量图 , 这个是创建应用时自带的配置
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}




2、布局文件


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <ImageView
        android:layout_width="100dip"
        android:layout_height="100dip"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_plane"/>
</androidx.constraintlayout.widget.ConstraintLayout>




3、运行效果



image.png





三、参考资料


参考文档 :


添加多密度矢量图形 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



博客资源 :


GitHub 项目源码 : https://github.com/han1202012/SVG


下载地址 : https://download.csdn.net/download/han1202012/18542570


目录
相关文章
|
4天前
|
Linux 编译器 Android开发
FFmpeg开发笔记(九)Linux交叉编译Android的x265库
在Linux环境下,本文指导如何交叉编译x265的so库以适应Android。首先,需安装cmake和下载android-ndk-r21e。接着,下载x265源码,修改crosscompile.cmake的编译器设置。配置x265源码,使用指定的NDK路径,并在配置界面修改相关选项。随后,修改编译规则,编译并安装x265,调整pc描述文件并更新PKG_CONFIG_PATH。最后,修改FFmpeg配置脚本启用x265支持,编译安装FFmpeg,将生成的so文件导入Android工程,调整gradle配置以确保顺利运行。
24 1
FFmpeg开发笔记(九)Linux交叉编译Android的x265库
|
3月前
|
安全 API Android开发
Android网络和数据交互: 解释Retrofit库的作用。
Android网络和数据交互: 解释Retrofit库的作用。
39 0
|
3月前
|
存储 算法 Android开发
AVB校验微观版本:android avb(Android Verified Boot)验证
AVB校验微观版本:android avb(Android Verified Boot)验证
232 0
|
23天前
|
Android开发
Android保存图片到相册(适配android 10以下及以上)
Android保存图片到相册(适配android 10以下及以上)
22 1
|
1月前
|
运维 监控 Java
应用研发平台EMAS产品常见问题之安卓构建版本失败如何解决
应用研发平台EMAS(Enterprise Mobile Application Service)是阿里云提供的一个全栈移动应用开发平台,集成了应用开发、测试、部署、监控和运营服务;本合集旨在总结EMAS产品在应用开发和运维过程中的常见问题及解决方案,助力开发者和企业高效解决技术难题,加速移动应用的上线和稳定运行。
|
1月前
|
监控 安全 Android开发
安卓发展历程和主要版本的简要介绍
安卓发展历程和主要版本的简要介绍
34 1
|
3月前
|
JSON Java Android开发
Android网络和数据交互: 请解释Android中的JSON解析库,如Gson。
Android网络和数据交互: 请解释Android中的JSON解析库,如Gson。
24 0
|
3月前
|
Android开发
Android源码学习(五):AVB2.0-libavb库介绍2
Android源码学习(五):AVB2.0-libavb库介绍2
105 0
|
3月前
|
Android开发
Android的一丢丢版本常识
Android的一丢丢版本常识
30 0
|
测试技术 Android开发
【Android 安装包优化】WebP 图片格式性能测试 ( 测试 WebP 图片解码速度 | 测试 WebP 图片编码速度 )
【Android 安装包优化】WebP 图片格式性能测试 ( 测试 WebP 图片解码速度 | 测试 WebP 图片编码速度 )
348 0
【Android 安装包优化】WebP 图片格式性能测试 ( 测试 WebP 图片解码速度 | 测试 WebP 图片编码速度 )