Android 性能优化到极致是什么样子的呢?APK极致优化,阿里大牛教你学(1)

使用如下:

<ImageView

android:layout_marginTop=“100dp”

android:layout_gravity=“center_horizontal”

android:layout_centerInParent=“true”

android:src=“@drawable/ic_icon_name”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

/>

或者

<ImageView

android:layout_marginTop=“100dp”

android:layout_gravity=“center_horizontal”

android:layout_centerInParent=“true”

app:srcCompat=“@drawable/ic_icon_name”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

/>

icon状态区分使用 Tint 着色器

=================================================================================

Tint着色器能够实现图片变色 ,利用Tint显示不同颜色的图片 ,在原本需要多张相同图片不同颜色的情况,能够减少apk的体积

UI效果如下:

注意了,这是同一张图片的不同效果

使用如下:

加上一行代码 android:tint=“@color/colorAccent”

<ImageView

android:layout_marginTop=“100dp”

android:layout_gravity=“center_horizontal”

android:layout_centerInParent=“true”

android:src=“@drawable/ic_icon_name”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:tint=“@color/colorAccent”

/>

需要多套不同尺寸的icon时,使用 svg

===================================================================================

Android studio 自带功能,可以自行配置需要的icon尺寸,打包时会自动生成对应尺寸的png 图片.

使用如下:

在app的build.graldle中的defaultConfig 标签下:

defaultConfig {

applicationId “com.example.apk”

minSdkVersion 19

targetSdkVersion 28

versionCode 1

versionName “1.0”

testInstrumentationRunner “android.support.test.runner.AndroidJUnitRunner”

//minSdkVersion 19 (5.0)

vectorDrawables.generatedDensities(‘xhdpi’,‘xxhdpi’,‘xxxhdpi’)

//minSdkVersion > 19

// vectorDrawables.useSupportLibrary = true

}

此时,drawable文件如下:

打包后如下:

以后APP内就只需要一套图就可解决多套图造成apk体积增大的问题了

App内大图压缩,使用webp格式图片

=================================================================================

WebP格式,谷歌开发的一种旨在加快图片加载速度的图片格式。图片压缩体积大约只有JPEG的2/3,并能节省大量的服务器宽带资源和数据空间。

使用如下:

转化前后对比

移除无用资源

====================================================================

一键移除 (不推荐)

一键移除未用到的资源,如果出现使用动态id加载资源会出现问题,而且这是物理删除,一旦删除将找不回了,所以能不用尽量别用,非要用请事先备份res文件.

使用如下:

![](https://imgconvert.csdnimg.cn/aHR0cHM6Ly91cGxvYWQtaW1hZ2VzLmppYW5zaHUuaW8vdXBsb2FkX2ltYWdlcy8xNTIxNzQ1Mi0yMTljZGJiOTA1MDE1MjAyCache_57f5cc0f73c34d73

资料获取→专栏
P2ltYWdlTW9ncjIvYXV0by1vcmllbnQvc3RyaXB8aW1hZ2VWaWV3Mi8yL3cvNTUwL2Zvcm1hdC93ZWJw?x-oss-process=image/format,png)

使用 shrinkResources 进行移除,配合 //Zipalign优化

使用 shrinkResources 必须先开启代码混淆 minifyEnabled

使用如下:

buildTypes {

release {

//开启代码混淆

minifyEnabled true

//Zipalign优化

zipAlignEnabled true

//移除无用的resource文件

shrinkResources true

proguardFiles getDefaultProguardFile(‘proguard-android-optimize.txt’), ‘proguard-rules.pro’

}

}

打包后效果如下:

虽然图片还存在. 但400多k的大小变成了2B

资源打包设置

======

由于第三方库的引入,如appcompat-v7的引入库中包含了大量的国际化资源,可根据自身业务进行相应保留和删除

原始包如下:

原始包中存在各国的语言,所以我们一般只需要保留中文即可,配置如下:

defaultConfig {

applicationId “com.zthx.xianglian”

minSdkVersion 19

targetSdkVersion 28

versionCode 1

versionName “1.0.0”

testInstrumentationRunner “android.support.test.runner.AndroidJUnitRunner”

//只保留指定和默认的资源

resConfigs(‘zh-rCN’,‘ko’)

}

  • 30
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值