Android-Iconics使用自定义的矢量图标库字体

2017-05-12  本文已影响0人  过眼云烟梦
public class MyApplication extends Application
{
    @Override
    public void onCreate()
    {
        super.onCreate();
        Iconics.init(getApplicationContext());
        Iconics.registerFont(new MyIconFont());
    }
public class MyIconFont implements ITypeface
{
    // 字体库的名字,在assets目录下
    private static final String TTF_FILE = "iconfont.ttf";
    private static Typeface typeface = null;
    private static HashMap<String, Character> mChars;

    @Override
    public IIcon getIcon(String key)
    {
        return Icon.valueOf(key);
    }

    @Override
    public HashMap<String, Character> getCharacters()
    {
        if (mChars == null)
        {
            HashMap<String, Character> aChars = new HashMap<String, Character>();
            for (Icon v : Icon.values())
            {
                aChars.put(v.name(), v.character);
            }
            mChars = aChars;
        }
        return mChars;
    }
    // icon名字的前缀
    @Override
    public String getMappingPrefix()
    {
        return "lbw";
    }
    @Override
    public String getFontName()
    {
        return "MyIconFont";
    }
    @Override
    public String getVersion()
    {
        return "1.0.0";
    }
    @Override
    public int getIconCount()
    {
        return mChars.size();
    }

    @Override
    public Collection<String> getIcons()
    {
        Collection<String> icons = new LinkedList<String>();
        for (Icon value : Icon.values())
        {
            icons.add(value.name());
        }
        return icons;
    }
    @Override
    public String getAuthor()
    {
        return "字库作者";
    }
    @Override
    public String getUrl()
    {
        return "字库URL下载地址";
    }
    @Override
    public String getDescription()
    {
        return "字库简介";
    }
    @Override
    public String getLicense()
    {
        return "字库许可证";
    }
    @Override
    public String getLicenseUrl()
    {
        return "许可证地址";
    }
    @Override
    public Typeface getTypeface(Context context)
    {
        if (typeface == null)
        {
            try
            {
                // 创建一个typeface,从asstes目录中读取
                typeface = Typeface.createFromAsset(context.getAssets(), TTF_FILE);
            } catch (Exception e)
            {
                return null;
            }
        }
        return typeface;
    }

    public enum Icon implements IIcon
    {
       // 对应编码的获取方法下面有讲到
        lbw_number_0('\ue621'),
        lbw_number_1('\ue622'),
        lbw_number_2('\ue62e'),
        lbw_number_3('\ue600'),
        lbw_number_4('\ue630'),
        lbw_number_5('\ue69b'),
        lbw_number_6('\ue70e'),
        lbw_number_7('\ue601'),
        lbw_number_8('\ue69e'),
        lbw_number_9('\ue62a');

        char character;
        Icon(char character)
        {
            this.character = character;
        }

        public String getFormattedName()
        {
            return "{" + name() + "}";
        }

        public char getCharacter()
        {
            return character;
        }

        public String getName()
        {
            return name();
        }

        private static ITypeface typeface;

        public ITypeface getTypeface()
        {
            if (typeface == null)
            {
                typeface = new MyIconFont();
            }
            return typeface;
        }
    }
}
 @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // 添加如下代码
        LayoutInflaterCompat.setFactory(getLayoutInflater(), new IconicsLayoutInflater(getDelegate()));
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
   // 重写此方法
    @Override
    protected void attachBaseContext(Context newBase)
    {
      super.attachBaseContext(IconicsContextWrapper.wrap(newBase));
    }
<Button
        android:layout_width="120dp"
        android:layout_height="60dp"
        android:text="{lbw-number-7} Button"/>

   <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:ico_color="@color/colorPrimary"
        app:ico_icon="lbw-number-2"
        app:ico_size="48dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="80dp"
        android:text="{lbw-number-6}双击666"
        android:textColor="@android:color/black"
        android:textSize="50sp"/>
IconicsDrawable iconicsDrawable = new IconicsDrawable(this)
                .icon(MyIconFont.Icon.lbw_number_0)
                .color(Color.YELLOW)
                .sizeDp(50);
        ImageView iv_font = (ImageView) findViewById(R.id.image_myfont);
        iv_font.setImageDrawable(iconicsDrawable);
 TextView tv_font = (TextView) findViewById(R.id.text_font);
        tv_font.setText("{lbw-number-6}{lbw-number-6}{lbw-number-6}");
        tv_font.setTextSize(100);
        tv_font.setTextColor(Color.YELLOW);

可以不用管它,运行是没有问题的,如果看着不开心,可以在根布局加上 ```
tools:ignore="MissingPrefix"

上一篇下一篇

猜你喜欢

热点阅读