首页>代码>android xml自定义配置文字字体>/MagicViews/src/com/ivankocijan/magicviews/MagicFont.java
package com.ivankocijan.magicviews;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Typeface;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Hashtable;
import java.util.List;

/**
 * Created by ivankocijan on 17.05.2014.
 */
public final class MagicFont {

    private AssetManager am;

    //list which contains all asset folders
    private ArrayList<String> assetFolders;

    //key = font name, value = fontPath
    private static final Hashtable<String, Typeface> fonts = new Hashtable<String, Typeface>();

    private static Context ctx;

    private static MagicFont magicFont;

    private static MagicFont getInstance (Context context) {

        if (magicFont == null) {
            magicFont = new MagicFont();
        }

        ctx = context;
        return magicFont;

    }

    private MagicFont () {
    }

    /**
     * Call this method to initialize app typefaces.
     *
     * @param typefaces typeface name. For example "cha_chicle.otf"
     */
    void initTypefaces (String... typefaces) {

        am = ctx.getResources().getAssets();

        if (assetFolders == null) {

            assetFolders = new ArrayList<String>();
            assetFolders.add(""); //root folder also needs to be checked
            getAssetFolders("");

        }


        setUpFonts(typefaces);

    }


    private void setUpFonts (String... typefaces) {

        for (String typeface : typefaces) {

            boolean fontFound = findFontInAssets(typeface);

            if (!fontFound) {
                throw new FontNotFoundException("Font " + typeface + " not found");
            }

        }

    }

    private boolean findFontInAssets (String typeface) {

        try {

            List<String> mapList;

            for (String assetFolder : assetFolders) {

                mapList = Arrays.asList(am.list(assetFolder));

                if (mapList.contains(typeface)) {

                    String fontPath;

                    if (assetFolder.isEmpty()) {
                        fontPath = typeface;
                    } else {
                        fontPath = assetFolder + "/" + typeface;
                    }

                    Typeface font = Typeface.createFromAsset(am, fontPath);

                    fonts.put(typeface, font);

                    return true;

                }

            }


        } catch (IOException e) {

            return false;

        }

        return false;

    }

    /**
     * This method goes through all assets folders and adds them to @assetFolders object
     *
     * @param path
     * @return
     */
    private boolean getAssetFolders (String path) {

        String[] list;


        try {

            list = ctx.getResources().getAssets().list(path);

            if (list.length > 0) {

                // This is a folder
                for (String file : list) {
                    assetFolders.add(file);
                    if (!getAssetFolders(path + "/" + file))
                        return false;
                }
            }

        } catch (IOException e) {
            return false;
        }

        return true;
    }

    /**
     * Returns requested typeface. If this class is not initialized it will try to initialaze it and return
     * requested font
     *
     * @param ctx
     * @param typeface
     * @return
     */
    public static Typeface getTypeface (Context ctx, String typeface) {

        if (!fonts.containsKey(typeface)) {
            MagicFont.getInstance(ctx).initTypefaces(typeface);
        }

        return fonts.get(typeface);

    }


}
最近下载更多
511076827  LV33 2022年4月10日
zzzxxcc  LV6 2020年6月17日
深圳-轻信  LV1 2019年1月22日
lttyoo  LV8 2018年12月18日
h1053556968  LV9 2018年7月13日
shylad  LV2 2017年10月11日
1062613427  LV2 2017年8月31日
_snail_  LV2 2016年12月26日
wfjaiowfoai  LV2 2016年11月10日
danielnj  LV25 2015年12月29日
最近浏览更多
萌了个乖乖  LV12 2022年5月21日
bingpine  LV6 2022年5月9日
511076827  LV33 2022年4月10日
tanjunhao  LV1 2022年3月4日
*财来  LV4 2021年12月27日
HOURENFENG  LV1 2021年3月4日
wyx065747  LV67 2021年1月3日
fengxiao  LV8 2020年12月29日
ali_iii 2020年9月16日
暂无贡献等级
mingzhu  LV6 2020年9月3日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友