android-iconify 使用详解 有图有真相

1、android-iconify简介

  • iconify的github地址:https://github.com/JoanZapata/android-iconify

  • 项目地址:http://joanzapata.com/android-iconify

  • iconify是一个矢量图标库,包含使用 Dave Gandy 制作的超过370中矢量字体图标,可以使Android应用开发者免于制作多种适用于不同屏幕大小尺寸的图片,从而提高开发者工作效率。

  • 适用场景:

    1、iconify原作者提供了三种他自定义的控件:IconTextView、IconButton、IconToggleButton,可以直接使用这三类控件来显示iconify中提供的字体图标;

2、在java代码中通过使用一个IconDrawable为具有setIcon(Drawable drawable)方法的控件设置该字体图标

  • 优点:由于这些图标均是矢量字体图标,所以不仅可以无限放大而不会失真,模糊,而且可以将适用于text的属性应用于这些矢量图标上,从而实现改变图标颜色、添加阴影等效果

  • 缺点:目前在xml文件中使用图标库中的资源时,需要自己对照查阅不同图标所对应的标记,自己手敲标记,这样不仅麻烦,而且容易出错。

2、使用android-iconify

2.1 添加依赖

在需要使用iconify的app的build.gradle的dependencies中添加依赖(下面添加了整个库,在实际开发过程中,可以只添加自己需要的某一个或几个库即可):

  1. dependencies {
  2. compile 'com.joanzapata.iconify:android-iconify-fontawesome:2.2.2' // (v4.5)
  3. compile 'com.joanzapata.iconify:android-iconify-entypo:2.2.2' // (v3,2015)
  4. compile 'com.joanzapata.iconify:android-iconify-typicons:2.2.2' // (v2.0.7)
  5. compile 'com.joanzapata.iconify:android-iconify-material:2.2.2' // (v2.0.0)
  6. compile 'com.joanzapata.iconify:android-iconify-material-community:2.2.2' // (v1.4.57)
  7. compile 'com.joanzapata.iconify:android-iconify-meteocons:2.2.2' // (latest)
  8. compile 'com.joanzapata.iconify:android-iconify-weathericons:2.2.2' // (v2.0)
  9. compile 'com.joanzapata.iconify:android-iconify-simplelineicons:2.2.2' // (v1.0.0)
  10. compile 'com.joanzapata.iconify:android-iconify-ionicons:2.2.2' // (v2.0.1)
  11. }

2.2 初始化android-iconify

自定义一个继承自Application类的类:

  1. public class MyApplication extends Application {
  2. @Override
  3. public void onCreate() {
  4. super.onCreate();
  5. Iconify
  6. .with(new FontAwesomeModule())
  7. .with(new EntypoModule())
  8. .with(new TypiconsModule())
  9. .with(new MaterialModule())
  10. .with(new MaterialCommunityModule())
  11. .with(new MeteoconsModule())
  12. .with(new WeathericonsModule())
  13. .with(new SimpleLineIconsModule())
  14. .with(new IoniconsModule());
  15. }
  16. }

2.3 配置Application

  1. <application
  2. android:name="com.application.MyApplication"
  3. android:icon="@mipmap/ic_launcher"
  4. android:label="@string/app_name">

2.4 在布局文件中的使用

其中fa xxxFont Awesome的图标库。icon-scan icon-wx-pay 是自定义阿里图标库

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="wrap_content">
  6. <LinearLayout
  7. android:layout_width="match_parent"
  8. android:layout_height="match_parent"
  9. android:layout_marginBottom="100dp"
  10. android:layout_marginTop="30dp"
  11. android:gravity="center"
  12. android:orientation="vertical">
  13. <TextView
  14. android:id="@+id/id_tv"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:text="example delegate"
  18. tools:ignore="HardcodedText" />
  19. <com.joanzapata.iconify.widget.IconTextView
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:shadowColor="#22000000"
  23. android:shadowDx="0"
  24. android:shadowDy="5"
  25. android:shadowRadius="1"
  26. android:text="Welcome {fa-smile-o} {fa-hand-peace-o} !"
  27. android:textColor="#2A9BDA"
  28. android:textSize="30sp" />
  29. <com.joanzapata.iconify.widget.IconTextView
  30. android:layout_width="wrap_content"
  31. android:layout_height="wrap_content"
  32. android:shadowColor="#22000000"
  33. android:shadowDx="3"
  34. android:shadowDy="3"
  35. android:shadowRadius="1"
  36. android:text="I {fa-heart-o} to {fa-code} on {fa-android}"
  37. android:textColor="#FF0000"
  38. android:textSize="40sp" />
  39. <com.joanzapata.iconify.widget.IconButton
  40. android:id="@+id/id_icon_ib"
  41. android:layout_width="wrap_content"
  42. android:layout_height="wrap_content"
  43. android:background="@null"
  44. android:clickable="true"
  45. android:shadowColor="#22000000"
  46. android:shadowDx="3"
  47. android:shadowDy="3"
  48. android:shadowRadius="1"
  49. android:text="I {fa-heart-o}"
  50. android:textColor="@color/selector_text_press_color"
  51. android:textSize="40sp" />
  52. <LinearLayout
  53. android:layout_width="match_parent"
  54. android:layout_height="wrap_content"
  55. android:layout_marginTop="20dp"
  56. android:gravity="center"
  57. android:orientation="horizontal">
  58. <ImageButton
  59. android:layout_width="50dp"
  60. android:layout_height="50dp"
  61. android:background="@drawable/alipay_button_style" />
  62. <ImageButton
  63. android:layout_width="60dp"
  64. android:layout_height="60dp"
  65. android:layout_marginLeft="30dp"
  66. android:background="@drawable/wx_button_style" />
  67. </LinearLayout>
  68. <LinearLayout
  69. android:layout_width="match_parent"
  70. android:layout_height="wrap_content"
  71. android:layout_marginTop="20dp"
  72. android:gravity="center"
  73. android:orientation="horizontal">
  74. <com.joanzapata.iconify.widget.IconTextView
  75. android:layout_width="50dp"
  76. android:layout_height="50dp"
  77. android:clickable="true"
  78. android:shadowColor="#22000000"
  79. android:text="{fa-weixin}"
  80. android:textColor="@color/selector_text_press_color"
  81. android:textSize="40sp" />
  82. <com.joanzapata.iconify.widget.IconTextView
  83. android:layout_width="50dp"
  84. android:layout_height="50dp"
  85. android:layout_marginLeft="30dp"
  86. android:clickable="true"
  87. android:text="{fa-cc-visa}"
  88. android:textColor="@color/selector_text_press_color"
  89. android:textSize="40sp" />
  90. </LinearLayout>
  91. <com.joanzapata.iconify.widget.IconTextView
  92. android:id="@+id/id_itv_wxicon"
  93. android:layout_width="wrap_content"
  94. android:layout_height="wrap_content"
  95. android:layout_marginTop="10dp"
  96. android:text="{icon-scan}"
  97. android:textSize="40sp" />
  98. <com.joanzapata.iconify.widget.IconTextView
  99. android:layout_width="wrap_content"
  100. android:layout_height="wrap_content"
  101. android:layout_marginTop="10dp"
  102. android:text="{icon-wx-pay}"
  103. android:textSize="40sp" />
  104. <LinearLayout
  105. android:layout_width="match_parent"
  106. android:layout_height="wrap_content"
  107. android:layout_marginBottom="30dp"
  108. android:layout_marginTop="10dp"
  109. android:gravity="center"
  110. android:orientation="horizontal">
  111. <com.joanzapata.iconify.widget.IconTextView
  112. android:layout_width="wrap_content"
  113. android:layout_height="wrap_content"
  114. android:text="{fa-cog}"
  115. android:textSize="30dp" />
  116. <com.joanzapata.iconify.widget.IconTextView
  117. android:layout_width="wrap_content"
  118. android:layout_height="wrap_content"
  119. android:layout_marginLeft="20dp"
  120. android:text="{fa-cog spin}"
  121. android:textSize="30dp" />
  122. <com.joanzapata.iconify.widget.IconTextView
  123. android:layout_width="wrap_content"
  124. android:layout_height="wrap_content"
  125. android:layout_marginLeft="20dp"
  126. android:text="{fa-refresh spin}"
  127. android:textSize="30dp" />
  128. <com.joanzapata.iconify.widget.IconTextView
  129. android:layout_width="wrap_content"
  130. android:layout_height="wrap_content"
  131. android:layout_marginLeft="20dp"
  132. android:text="{fa-spinner spin}"
  133. android:textSize="30dp" />
  134. <ImageView
  135. android:id="@+id/id_iv"
  136. android:layout_width="wrap_content"
  137. android:layout_height="wrap_content"
  138. android:layout_marginLeft="20dp" />
  139. </LinearLayout>
  140. </LinearLayout>
  141. </ScrollView>

3、自定义FontModule

3.1 下载资源

3.2 添加资源自定义FontModule

将上文截图中的 iconfont.ttf 复制到assets目录

自定义FontModule可以参考FontAwesomeModule,需要实现IconFontDescriptor 接口以及实现Icon接口来添加我们具体的图标

  1. public class IconFontModule implements IconFontDescriptor {
  2. @Override
  3. public String ttfFileName() {
  4. return "iconfont.ttf";//上文复制的字体文件
  5. }
  6. @Override
  7. public Icon[] characters() {
  8. return IconFonts.values();
  9. }
  10. }
  1. public enum IconFonts implements Icon {
  2. //&#xe609 将/&#x替换成\u
  3. icon_scan('\ue609'),
  4. icon_ali_pay('\ue65e'),
  5. icon_wx_pay('\ue615'),
  6. icon_qq_pay('\ue60d');
  7. private char character;
  8. IconFonts(char character) {
  9. this.character = character;
  10. }
  11. @Override
  12. public String key() {
  13. return name().replace('_', '-');
  14. }
  15. @Override
  16. public char character() {
  17. return character;
  18. }
  19. }

4、在代码中使用

  1. IconDrawable iconDrawable = new IconDrawable(this, FontAwesomeIcons.fa_key)
  2. .colorRes(R.color.colorAccent)
  3. .actionBarSize();
  4. imageView.setImageDrawable(iconDrawable);

5、使用到的资源文件

支付宝默认状态 支付宝点击状态 微信正常状态 微信点击状态

selector_text_press_color.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:state_pressed="true" android:color="@color/colorAccent"/>
  4. <item android:color="@color/colorGreen"/>
  5. </selector>

alipay_button_style.xml

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:state_pressed="true" android:drawable="@drawable/alipay_pressed" />
  4. <item android:drawable="@drawable/alipay_normal" />
  5. </selector>

wx_button_style.xml

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:state_pressed="true" android:drawable="@drawable/wx_pressed" />
  4. <item android:drawable="@drawable/wx_normal" />
  5. </selector>

colors.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <color name="colorPrimary">#3F51B5</color>
  4. <color name="colorPrimaryDark">#303F9F</color>
  5. <color name="colorAccent">#FF4081</color>
  6. <color name="colorGreen">#04b00f</color>
  7. </resources>

android-iconify 使用详解的更多相关文章

  1. android:ToolBar详解

    android:ToolBar详解(手把手教程) 泡在网上的日子 发表于 2014-11-18 12:49 第 124857 次阅读 ToolBar 42 来源 http://blog.mosil.b ...

  2. Android之canvas详解

    首先说一下canvas类: Class Overview The Canvas class holds the "draw" calls. To draw something, y ...

  3. 【转】Android Canvas绘图详解(图文)

    转自:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2012/1212/703.html Android Canvas绘图详解(图文) 泡 ...

  4. Android 核心分析 之八Android 启动过程详解

    Android 启动过程详解 Android从Linux系统启动有4个步骤: (1) init进程启动 (2) Native服务启动 (3) System Server,Android服务启动 (4) ...

  5. Android GLSurfaceView用法详解(二)

    输入如何处理       若是开发一个交互型的应用(如游戏),通常需要子类化 GLSurfaceView,由此可以获取输入事件.下面有个例子: java代码: package eoe.ClearTes ...

  6. Android编译过程详解(一)

    Android编译过程详解(一) 注:本文转载自Android编译过程详解(一):http://www.cnblogs.com/mr-raptor/archive/2012/06/07/2540359 ...

  7. android屏幕适配详解

    android屏幕适配详解 官方地址:http://developer.android.com/guide/practices/screens_support.html 一.关于布局适配建议 1.不要 ...

  8. Android.mk文件详解(转)

    源:Android.mk文件详解 从对Makefile一无所知开始,折腾了一个多星期,终于对Android.mk有了一个全面些的了解.了解了标准的Makefile后,发现Android.mk其实是把真 ...

  9. Android Studio 插件开发详解四:填坑

    转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/78265540 本文出自[赵彦军的博客] 在前面我介绍了插件开发的基本流程 [And ...

  10. Android Studio 插件开发详解三:翻译插件实战

    转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/78113868 本文出自[赵彦军的博客] 一:概述 如果不了解插件开发基础的同学可以 ...

随机推荐

  1. Ubuntu下用cue文件对ape和wav文件自动分轨

    很多音乐CD的镜像文件都带cue格式的分轨文件,在Ubuntu Linux下可以通过一些工具来实现自动分轨. 一.Ubuntu下需要安装的工具有flac,shntool,libav-tools $ s ...

  2. springMVC 配置和使用

    springMVC相对于Struts2学习难度较为简单,并且更加灵活轻便. 第一步:导入jar包 spring.jar.spring-webmvc.jar.commons-logging.jar.sp ...

  3. ex2:逻辑回归及正则条件下的练习

    EX2 逻辑回归练习 ​ 假设你是一个大学某系的管理员,你想根据两项考试结果来确定每个申请人的录取机会.你有以前申请人的历史资料以作为逻辑回归的训练集.对于每一个训练集,你拥有每个申请人的两项考试的分 ...

  4. java中的vo、dto 、dao

    VO是跟数据库里表的映射,一个表对应一个VO  DAO是用VO来访问真实的表,对数据库的操作都在DAO中完成  BO是业务层,做逻辑处理的 VO , PO , BO , QO, DAO ,POJO  ...

  5. 盒子模型,定位技术,负边距,html5 新增标签

    盒子模型 /*[margin 外边距] margin属性最多四个 1.只写一个值,四个方向的margin均为这个值 2.写两个值:上,右两个方向,下默认=上,右 默认=左 3.写三个值:上.右.下三个 ...

  6. 《基于Node.js实现简易聊天室系列之详细设计》

    一个完整的项目基本分为三个部分:前端.后台和数据库.依照软件工程的理论知识,应该依次按照以下几个步骤:需求分析.概要设计.详细设计.编码.测试等.由于缺乏相关知识的储备,导致这个Demo系列的文章层次 ...

  7. HTML5+CSS3实现的响应式垂直时间轴

    <!DOCTYPE HTML><html><head><meta charset="utf-8"><meta name=&qu ...

  8. Unity 游戏框架搭建 (二) 单例的模板

      上一篇文章中说到的manager of managers,其中每个manager都是单例的实现,当然也可以使用静态类实现,但是相比于静态类的实现,单例的实现更为通用,可以适用大多数情况. 如何设计 ...

  9. Chrome控制台使用详解

    Chrome的开发者工具已经强大到没朋友的地步了,特别是其功能丰富界面友好的console,使用得当可以有如下功效: 更高「逼格」更快「开发调试」更强「进阶级的Frontender」 Bug无处遁形「 ...

  10. ES语法注意事项

    在函数内部定义全局变量:举个栗子 function fn(){ var str = "hezhi"; } -alert(str) //=>fn不执行的 =>str is ...