Android高级开发-APK极致优化

栏目: Android · 发布时间: 7年前

内容简介:使用矢量图代替位图可以减小 APK 的尺寸,因为可以针对不同屏幕密度调整同一文件的大小,而不会降低图像质量。

blog.csdn.net/megatronkin…

developer.android.google.cn/studio/writ…

使用矢量图代替位图可以减小 APK 的尺寸,因为可以针对不同屏幕密度调整同一文件的大小,而不会降低图像质量。

矢量图首次加载时可能消耗更多的 CPU 资源。之后,二者的内存使用率和性能则不相上下。我们建议您将矢量图像限制为最大 200 x 200 dp;否则,绘制它可能需要耗费很长的时间。

SVG是由XML定义的,标准SVG根节点是<svg>在Android中通过Vector实现支持,根节点是<vector>

Android高级开发-APK极致优化
Android高级开发-APK极致优化
Android高级开发-APK极致优化

版本差异(具体参考官方文档)

Android 5.0(API 级别 21)及更高版本会提供矢量图支持。如果应用的最低 API 级别低于以上版本,Vector Asset Studio 会将矢量图文件添加到项目中;另外,在构建时,Gradle 会创建不同分辨率的 PNG 光栅图像。

对于 Android 5.0(API 级别 21)及更高版本,Vector Asset Studio 支持所有 VectorDrawable 元素。为向后兼容 Android 4.4(API 级别 20)及更低版本,Vector Asset Studio 支持部分 XML 元素。

2. Tint着色器 yifeng.studio/2017/03/30/…

自 API 21 (Android L)开始,Android SDK 引入 tint 着色器,可以随意改变安卓项目中图标或者 View 背景的颜色,一定程度上可以减少同一个样式不同颜色图标的数量,从而起到 Apk 瘦身的作用。

Android高级开发-APK极致优化

tint 或 backgroundTint 属性,与 src 或 background 属性一定是对应成对出现的。

Android高级开发-APK极致优化
Android高级开发-APK极致优化
<!-- This color state list defines the color applied to the "buttonbackground" drawable.
  A color is selected based on the state of its view.-->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Pressed state -->
    <item android:color="@color/red" android:state_pressed="true" />
    <!-- Focused state -->
    <item android:color="@color/light_purple" android:state_focused="true" />
    <item android:color="@color/deep_purple" android:state_enabled="true" />
    <!-- Disabled state -->
    <item android:color="@color/light_blue" android:state_enabled="false" />
    <!-- Default -->
    <item android:color="@color/light_blue" />
</selector>
复制代码
Android高级开发-APK极致优化
Android高级开发-APK极致优化

java代码中使用DrawableCompat

Drawable orgiDrawable = ContextCompat.getDrawable(this, R.drawable.btn_default_normal_holo);
Drawable tintDrawable = DrawableCompat.wrap(orgiDrawable).mutate();
DrawableCompat.setTint(tintDrawable, Color.parseColor("#00ff00"));

ImageView backTintImg = (ImageView)findViewById(R.id.tint);
backTintImg.setImageDrawable(tintDrawable);

// ---------------------------------------------

// 必须重新获取下对象
Drawable orgiDrawable2 = ContextCompat.getDrawable(this, R.drawable.btn_default_normal_holo);
Drawable tintlistDrawable = DrawableCompat.wrap(orgiDrawable2).mutate();
DrawableCompat.setTintList(tintlistDrawable, ContextCompat.getColorStateList(this, R.color.custom_tint));
DrawableCompat.setTintMode(tintlistDrawable, PorterDuff.Mode.MULTIPLY);

ImageView tintListImg = (ImageView)findViewById(R.id.tintlist);
tintListImg.setImageDrawable(tintlistDrawable);

// ---------------------------------------------
ImageView colorfilterImg = (ImageView)findViewById(R.id.colorfilter);
colorfilterImg.setColorFilter(new PorterDuffColorFilter(Color.parseColor("#ccffcc"), PorterDuff.Mode.MULTIPLY));
复制代码

3. 资源打包配置

Android Gradle还为我们 提供了一个resConfigs,它属于ProductFlavor的一个方法,可以让我们配置哪些类型的资源才被打包到Apk中,比如只有中文的,只有hdpi格式的图片等等,这是非常重要的,比如我们引用的第三方库,特别是Support Library 和 Google Play Services这两个主要的大库,因为国际化的问题,他们都支持了几十种语言,但是对于我们的App来说,我们并不需要这么多,比如我们只用中文的语言就可以了,其他的都不需要。

Android高级开发-APK极致优化
build.gradle
    
    defaultConfig {
        ……
        resConfigs 'zh', 'en’,'ja'
    }
设置后生成的APK资源。系统默认语言是英语en
复制代码
Android高级开发-APK极致优化

4. 配置不同CPU的so库

build.gradle
  
   defaultConfig {
        ndk {
            abiFilters(‘armeabi')
        }
    }
复制代码

5. 删除无用资源(lint)

AndroidStudio Analyze->Run Inspection by Name->Unused resources
复制代码

6. 代码混淆

gradle属性 minifyEnabled
混淆配置 -dontwarn -keep
复制代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Computer Age Statistical Inference

Computer Age Statistical Inference

Bradley Efron、Trevor Hastie / Cambridge University Press / 2016-7-21 / USD 74.99

The twenty-first century has seen a breathtaking expansion of statistical methodology, both in scope and in influence. 'Big data', 'data science', and 'machine learning' have become familiar terms in ......一起来看看 《Computer Age Statistical Inference》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具