开发小技巧-图片资源管理

Android图片资源管理的几种方案

前言

一个完整的项目离不开图片资源,在一个Apk的组成中,大头都是资源文件了,如果资源太多导致Apk太大,如何管理资源图片都是一个App开发者头痛的问题!除了那些资源都通过后台下发的...😂😂

下面介绍几种目前常见的图片资源管理方案:

一、png图片

相信大家刚接触 Android 开发都是用png图片了,并且按照分辨率分为三、四种尺寸 hdpi xhdpi xxhdpi xxxhdpi等。 如果你只使用一种尺寸,那么在匹配的其他分辨率设备上就会拉伸/压缩图片。

如果图片使用不当就会占用更多的内存空间,更容易导致OOM。

同一张图片分别放在不同drawable文件夹(drawable-hdpi, drawable-xhdpi, drawable-xxhdpi)下,相同的dpi下的文件夹下加载出来的图片,bitmap占用内存大小一样。

对于同一张图片,放在不同手机、不同的屏幕密度文件夹下,占用的空间都不相同。

以mdpi为例,mdpi下面一个图标的像素对应 hdpi的像素 对应xhdpi的像素 对应xxhdpi的像素 mdpi - 21 * 21
hdpi - 32 * 33 是m的1.5倍 xhdpi - 42 * 43 是m的2倍 xxhdpi - 63 * 64 是m的3倍

Android查找图片也是比较智能,如果没有对应分辨率的图片则会先往上找,找不到在往下找。如果找到了就会对图片进行放大相应的倍数。

公式为:占用内存=图片宽度 X 图片高度/((资源文件夹密度/手机屏幕密度)^2) * 色彩格式每一个像素占用字节数。

所以策略就是对应的分辨率对于相对的图片,如果不想放那么多图片,那么尽量放高分辨率的图,如xhdpi xxhdpi的。但是高分辨的图片往往占用空间很大,所以要压缩图片。

二、webp图片

相比png,webp格式的图片更节省空间,可以减少Apk的大小,我们一般有2种方式转换。

现在AS可以很方便的把一个png图片转换为webp的图片,直接点击res文件夹右键,可以直接转换整个res目录,如果想精准的转换指定drawable,那么可以直接对drawable文件夹右键。如下

点击转换然后一路next,就能转换完成。

另一种方式是通过gradle插件,在打包的时候动态生成webp文件,而原始图片还是png。比较出名的是 booster 插件。

 booster_version = '2.4.0'

 classpath "com.didiglobal.booster:booster-gradle-plugin:$booster_version"

 classpath "com.didiglobal.booster:booster-task-compression-cwebp:$booster_version"

在App的gradle下面 使用插件 apply plugin: 'com.didiglobal.booster' 那么在编译的时候就能生成webp图片,具体的使用看这里

三、svg图片

如果担心太多的分辨率图片会导致Apk变大,那我们可以使用SVG矢量图,只需要一个图片,可以无损的放大与缩小。

比如我们从设计师那里要来svg的图片,如下:

导入到项目中

 

svg会直接报错,我们需要转为xml描述文件,也就是 SVG to VectorDrawable 。 网上很多工具,这里我使用一个我自用的工具

 

使用的时候拖进去,点击下载就完成了。

 

xml文件导入到drawable文件夹中,预览如下: 


 

是的这样导入的svg是带颜色的!

在项目的build.gradle中配置 vectorDrawables.useSupportLibrary = true 之后我们现在可以直接使用了。

ImageView 与 AppCompatImageView 都可以使用。代码如下:

       <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/d_15dp"
            android:orientation="horizontal">

            <ImageView
                android:layout_width="40dp"
                android:layout_height="@dimen/d_40dp"
                android:src="@drawable/kafei" />

            <ImageView
                android:layout_width="40dp"
                android:layout_height="@dimen/d_40dp"
                android:src="@drawable/kele" />

            <ImageView
                android:layout_width="40dp"
                android:layout_height="@dimen/d_40dp"
                android:src="@drawable/shutiao" />

            <ImageView
                android:layout_width="40dp"
                android:layout_height="@dimen/d_40dp"
                android:src="@drawable/xuegao" />

            <ImageView
                android:layout_width="40dp"
                android:layout_height="@dimen/d_40dp"
                android:src="@drawable/zhenzhunaicha" />

            <androidx.appcompat.widget.AppCompatImageView
                app:srcCompat="@drawable/mianhuatang"
                android:layout_width="40dp"
                android:layout_height="40dp"/>

        </LinearLayout>

效果:

使用SVG图片相比png与webp图片来说,会更一步减少APK体积,无需多套对应分辨率的图片。可以说是非常方便。

四、iconfont

Android中同样可以指定字体文件,我们可以在iconfont网站下载图片,如果是自己的设计我们同样可以上传到iconfont自己的项目中,然后再下载代码。

核心是通过iconfont下载代码的方式,图片的来源可以是开源的,也可以是自己设计的。

下载解压之后文件如下: 

 

我们需要用到的index.html 里面定义了图片代码,iconfont.ttf是我们需要导入到项目中的。我们需要把iconfont.ttf导入到项目的assets目录下。

然后我们定义一个自定义的TextView专门用于加载iconfont。

public class IconFontTextView extends AppCompatTextView {

    public IconFontTextView(@NonNull Context context) {
        super(context);
        init(context);
    }

    public IconFontTextView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public IconFontTextView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        setTypeface(FontCache.getIconFontTypeFace("", context));
    }

}


public class FontCache {

    public static Typeface iconfont;

    //获取IconFont的TypeFace
    public static Typeface getIconFontTypeFace(String iconfontNum, Context context) {
        if (iconfont != null) {
            return iconfont;
        } else {
            iconfont = Typeface.createFromAsset(context.getAssets(), "iconfont" + iconfontNum + ".ttf");
        }
        return iconfont;
    }

}

我们就可以直接使用TextView来加载字体文件了。打开上面说的字体文件压缩文件中的index.html

我们直接使用unicode的字符串就行了。代码如下:

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/d_15dp"
            android:orientation="horizontal">

            <com.guadou.lib_baselib.font_text_view.IconFontTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="&#xe65e;"
                android:textColor="@color/gray"
                android:textSize="@dimen/d_40dp" />

            <com.guadou.lib_baselib.font_text_view.IconFontTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="&#xe660;"
                android:textColor="#E91E63"
                android:textSize="@dimen/d_40dp" />

            <com.guadou.lib_baselib.font_text_view.IconFontTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="&#xe661;"
                android:textColor="#009688"
                android:textSize="@dimen/d_40dp" />

            <com.guadou.lib_baselib.font_text_view.IconFontTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="&#xe690;"
                android:textColor="#FF9800"
                android:textSize="@dimen/d_40dp" />


            <com.guadou.lib_baselib.font_text_view.IconFontTextView
                android:id="@+id/tv_icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#00BCD4"
                android:textSize="@dimen/d_40dp" />

        </LinearLayout>

效果:

需要注意的是在xml中直接使用unicode是可以的,但是在Java中直接使用是不行的。

      mBinding.tvIcon.text = "&#xe6cc;"

效果为:

 

我们需要知道e6cc才是它真正的代码,在Java中设置的话需要改为 

 

这样才不会报错,如果格式错误会报红,编译无法通过的。

如果是我们自己上传的svg图片,记得需要去掉图片颜色。传入svg图片之后,在我的图标中点击下载代码就能使用了,和上面的办法是一样的。

自定义SVG图片上传,并下载iconfont代码之后效果如下:

 

缺点是无法当前显示,需要编译之后才能在预览页面展示出图标的样式:

 

在编译通过之后就能正常的显示图片: 

 

这种方式可以认为是svg的升级版,并且它的占用空间比SVG的图片更小,例如我们iconfont.ttf 中定义了10个SVG图片,但是他的大小为11k,有时候我们一个svg的图片大小就可能为10k,可以说是极致的压缩了apk大小。

并且还有一个好处是无需资源混淆,天然自带资源混淆,你绝对无法在 iconfont.ttf 文件中找到我的图片资源。比微信的res混淆方案更优秀。

缺点是使用麻烦,同时需要在iconfont中创建项目管理资源,如果资源更新或者删除,无法精确的管理。不想png,svgn那样,我可以直接找到未使用的资源删除掉,随着版本的迭代,资源的更新换代,iconfont的方式管理资源会比较麻烦!

总结

上面的几种方案,各有利弊,这里也只是介绍和探讨了几种方案的利弊和基本的实现。在实际项目开发中大家可以按需选择即可。

本文源码在此!如果有更多的更好的方案,可以留言区讨论!


作者:newki
链接:https://juejin.cn/post/7111221172297007118
来源:稀土掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值