Android 动态使用Iconfont图标

该博客介绍了如何在Android应用中动态加载并使用Iconfont图标,避免因Unicode编码改变而频繁修改XML文件。主要步骤包括:从后台下载Iconfont.ttf和iconfont.json文件,解析json获取图标Unicode编码,然后将编码转换为字符用于显示。
摘要由CSDN通过智能技术生成

Android 动态使用Iconfont图标


Android 动态使用Iconfont图标

问题

每当进行增删操作后,Iconfont图标文件中Unicode编码可能改变。如何保证进行以上操作后不需要手动变更xml文件中Iconfont字符对照表?

思路

1、通过后台网络下载Iconfont.ttficonfont.json两个文件(文件来自阿里巴巴Iconfont库中Iconfont资源压缩包),可设计版本管理。
2、解析iconfont.json文件,获取Iconfont图标集glyphs
3、通过 namefont_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 "?";
        }
    }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值