Android 动态使用Iconfont图标
Android 动态使用Iconfont图标
问题
每当进行增删操作后,Iconfont图标文件中Unicode编码可能改变。如何保证进行以上操作后不需要手动变更xml文件中Iconfont字符对照表?
思路
1、通过后台网络下载Iconfont.ttf
和iconfont.json
两个文件(文件来自阿里巴巴Iconfont库中Iconfont资源压缩包),可设计版本管理。
2、解析iconfont.json
文件,获取Iconfont图标集glyphs
。
3、通过 name
或 font_class
(UI设计时这些图标时名字是不变固定的)获取glyphs
集合中所需图标对象,进而获取其Unicode编码。
4、Unicode编码转字符。
附
1、iconfont.json
的数据格式
public class IconFontBean {
private String id;
private String name;
private String font_family;
private String css_prefix_text;
private String description;
private List<IconFontBean.GlyphsBean> glyphs;
public IconFontBean() {
}
public String getId() {
return this.id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getFont_family() {
return this.font_family;
}
public void setFont_family(String font_family) {
this.font_family = font_family;
}
public String getCss_prefix_text() {
return this.css_prefix_text;
}
public void setCss_prefix_text(String css_prefix_text) {
this.css_prefix_text = css_prefix_text;
}
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
public List<IconFontBean.GlyphsBean> getGlyphs() {
return this.glyphs;
}
public void setGlyphs(List<IconFontBean.GlyphsBean> glyphs) {
this.glyphs = glyphs;
}
public static class GlyphsBean {
private String icon_id;
private String name;
private String font_class;
private String unicode;
private int unicode_decimal;
public GlyphsBean(String name) {
this.name = name;
}
public String getIcon_id() {
return this.icon_id;
}
public void setIcon_id(String icon_id) {
this.icon_id = icon_id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getFont_class() {
return this.font_class;
}
public void setFont_class(String font_class) {
this.font_class = font_class;
}
public String getUnicode() {
return this.unicode;
}
public void setUnicode(String unicode) {
this.unicode = unicode;
}
public int getUnicode_decimal() {
return this.unicode_decimal;
}
public void setUnicode_decimal(int unicode_decimal) {
this.unicode_decimal = unicode_decimal;
}
public boolean equals(Object o) {
if (this == o) {
return true;
} else if (o != null && this.getClass() == o.getClass()) {
IconFontBean.GlyphsBean that = (IconFontBean.GlyphsBean)o;
return this.font_class.equals(that.font_class);
} else {
return false;
}
}
public int hashCode() {
return Objects.hash(new Object[]{this.font_class});
}
}
}
2、 通过font_class
获取Iconfont
public static IconFontBean getIconFontBean() {
try {
if (iconFontBean == null) {
//示例的Iconfont.ttf和iconfo.json放在asstes文件中
AssetManager assets = BaseApplication.instance.getAssets();
if (assets != null) {
InputStream ignored = assets.open("iconfont.json");
StringBuilder iconFontJson = new StringBuilder();
byte[] buffer = new byte[1024];
for(int len = ignored.read(buffer); len > 0; len = ignored.read(buffer)) {
iconFontJson.append(new String(buffer, 0, len));
}
ignored.close();
//这里通过Gson解析json
iconFontBean = (IconFontBean)gson().fromJson(iconFontJson.toString(), IconFontBean.class);
}
}
return iconFontBean;
} catch (IOException var5) {
var5.printStackTrace();
return null;
}
}
public static String getIconFont(String font_class) {
try {
IconFontBean iconFontBean = getIconFontBean();
List<GlyphsBean> glyphs = iconFontBean.getGlyphs();
//查找所需图标对象
GlyphsBean glyphsBean = (GlyphsBean)glyphs.get(glyphs.indexOf(new GlyphsBean(font_class)));
return String.valueOf((char)Integer.parseInt(glyphsBean.getUnicode(), 16));//Unicode编码转字符串
} catch (NumberFormatException var4) {
var4.printStackTrace();
return "?";
}
}