32

Android安装包精简系列之图标转字体

 3 years ago
source link: https://blog.bihe0832.com/android-optimize-icontofont.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

这是个人总结的应用安装精简系列的关于图片精简的第一篇,主要介绍怎么使用图标字体库来代替图标达到精简图片的效果

在应用会存在很多简单的按钮,一般这些按钮都会使用png的图片,虽然这部分图片并不会占用太多的资源,但是也是可以优化的内容,就以这部分的优化作为系列的开始。

除 .9图、带阴影图外,所有常见的图标都做到字体库里,不使用icon图片。目前比较成熟的图标字体库可以使用font-awsome。Font Awesome为您提供可缩放的矢量图标,您可以使用CSS所提供的所有特性对它们进行更改,包括:大小、颜色、阴影或者其它任何支持的效果。

具体方案中包含了,怎么下载、怎么修改、怎么使用。

下载字体库

在Font Awesome的官网http://fontawesome.dashgame.com/下载最新的库,解压后在fonts目录下找到fontawesome-webfont.ttf,然后将字体库拷贝到项目的assets目录

使用字体库

创建字库类

public class FontManager {

    public static final String FONTAWESOME = "fontawesome-webfont.ttf";

    public static Typeface getTypeface(Context context) {
        return Typeface.createFromAsset(context.getAssets(), FONTAWESOME);
    }

}
final TextView icon1 = (TextView) findViewById(R.id.icon1);
icon1.setTypeface(FontManager.getTypeface(this));

fontawesome-webfont.ttf完整的字体库有160多K,如果项目用到的icon数量极少的时候,貌似切换为字体得不偿失。这里我们可以通过删除字体库里面的无用图标来达到我们的需求,具体方法如下:




About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK