iconify 使用方式

壤驷穆冉
2023-12-01

1. github 地址

https://github.com/JoanZapata/android-iconify

2. 建立依赖方式

    dependencies {
        compile 'com.joanzapata.iconify:android-iconify-fontawesome:2.2.2' // (v4.5)
        compile 'com.joanzapata.iconify:android-iconify-entypo:2.2.2' // (v3,2015)
        compile 'com.joanzapata.iconify:android-iconify-typicons:2.2.2' // (v2.0.7)
        compile 'com.joanzapata.iconify:android-iconify-material:2.2.2' // (v2.0.0)
        compile 'com.joanzapata.iconify:android-iconify-material-community:2.2.2' // (v1.4.57)
        compile 'com.joanzapata.iconify:android-iconify-meteocons:2.2.2' // (latest)
        compile 'com.joanzapata.iconify:android-iconify-weathericons:2.2.2' // (v2.0)
        compile 'com.joanzapata.iconify:android-iconify-simplelineicons:2.2.2' // (v1.0.0)
        compile 'com.joanzapata.iconify:android-iconify-ionicons:2.2.2' // (v2.0.1)
    }

3. 初始化方式

    public class MyApplication extends Application {

        @Override
        public void onCreate() {
            super.onCreate();
            Iconify
                .with(new FontAwesomeModule())
                .with(new EntypoModule())
                .with(new TypiconsModule())
                .with(new MaterialModule())
                .with(new MaterialCommunityModule())
                .with(new MeteoconsModule())
                .with(new WeathericonsModule())
                .with(new SimpleLineIconsModule())
                .with(new IoniconsModule());
        }
    }

4. xml 文件中调用

用 {} 符号将标签包裹起来,即可使用

    <com.joanzapata.iconify.widget.IconTextView
        android:text="I {fa-heart-o} to {fa-code} on {fa-android}"
        android:shadowColor="#22000000"
        android:shadowDx="3"
        android:shadowDy="3"
        android:shadowRadius="1"
        android:textSize="40sp"
        android:textColor="#FF..."
        ... />

输出结果:

I 爱心图片 to 代码图片 on 安卓图片

5. 基础调整

  • 调整大小的方式

    • {fa-code 12px}
    • {fa-code 12dp}
    • {fa-code 12sp}
    • {fa-code @dimen/my_text_size}
    • {fa-code 120%}
  • 调整颜色的方式

    • {fa-code #RRGGBB}
    • {fa-code #AARRGGBB}
    • {fa-code @color/my_color}
  • 让图标旋转

    • {fa-cog spin}

6. java 中调用

    // Set an icon in the ActionBar
    menu.findItem(R.id.share).setIcon(
       new IconDrawable(this, FontAwesomeIcons.fa_share)
       .colorRes(R.color.ab_icon)
       .actionBarSize());

7. 加入自定义图标

  • 第一步 需要另外添加依赖

    implementation 'com.joanzapata.iconify:android-iconify:2.2.2'
    
  • 第二步 实现 IconFontDescriptor 类

  • 第三步 从网上下载 .ttf 文件,并放到项目的 assets 目录下

代码实现如下(assets 目录下有 iconfront.ttf):

  • FrontIconModule.java

    package com.thorn.milk.ec.icon;
    
    import com.joanzapata.iconify.Icon;
    import com.joanzapata.iconify.IconFontDescriptor;
    
    public class FrontIconModule implements IconFontDescriptor {
        @Override
        public String ttfFileName() {
            return "iconfront.ttf";
        }
    
        @Override
        public Icon[] characters() {
            return EcIcon.values();
        }
    }
    
  • EcIcon.java 枚举类,其实就是将图标的名称的下划线换成中划线

    package com.thorn.milk.ec.icon;
    
    public enum EcIcon implements com.joanzapata.iconify.Icon {
    
        icon_ali_pay('\ue606'),
        icon_scan('\ue606');
        private char character;
    
        EcIcon(char character) {
            this.character = character;
        }
    
        @Override
        public String key() {
            return name().replace('_', '-');
        }
    
        @Override
        public char character() {
            return character;
        }
    }
    
 类似资料: