Android 如何将SVG做成字体图标的(iconify)使用
框架 GitHub:https://github.com/JoanZapata/android-iconify
//字体图标
implementation 'com.joanzapata.iconify:android-iconify-ionicons:2.2.2'
implementation 'com.joanzapata.iconify:android-iconify-fontawesome:2.2.2'
到阿里巴巴 矢量图网站 地址:http://www.iconfont.cn/home/index?spm=a313x.7781069.1998910419.2
搜索想要的图标保存到购物车
到购物车中点击下载代码
下载代码得到zip 压缩包
解压得到这样的文件
将iconfont.ttf保存到Android 项目的资产目录(assets)下
接下来就是如何在代码中使用了。
创建两个封装类
public enum EcIcons implements Icon {
//枚举请下面视图
// icon_location('\ue601');
icon_device('\ue602'),
icon_files('\ue603'),
icon_monitor('\ue604'),
icon_home('\ue605'),
icon_record('\ue606'),
icon_zhuye('\ue76e');
private char character;
EcIcons(char character) {
this.character = character;
}
@Override
public String key() {
return name().replace('_', '-');
}
@Override
public char character() {
return character;
}
}
icon_zhuye() 枚举名请参考
'\ue76e' 请参考 下图
'\ue76e' 这样的编码是怎么来的呢,就是按下图来的:将 改成 '\ue76e'
好了枚举设好了,设置引用类
public class FontEcModule implements IconFontDescriptor {
@Override
public String ttfFileName() {
return "iconfont.ttf";
}
@Override
public Icon[] characters() {
return EcIcons.values();
}
}
封装类已经设置好了,开始使用了
1.初始化
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Iconify.with(new FontEcModule());
Iconify.with(new FontAwesomeModule());
}
}
2.只要这样就可以使用啦
(1)代码中使用
tv_title.setText("{icon-zhuye}");
(2)布局中使用
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="@dimen/x36"
android:background="@drawable/bg_dialog_view_title"
android:gravity="center"
android:text="{icon-zhuye}"
android:textColor="@color/while1"
android:textSize="@dimen/x13" />
当然,在开发的过程中,往往都是使用设计师给的图片,那么如何做字体icon呢,直接让设计提供SVG图片,之后手动将设计给的SVG上传到阿里巴巴的矢量图网站上去,之后又通过矢量图网站下载代码下来就OK了,直接按着上面的步骤做就可以了