1、iconify簡介java
iconify的github地址:https://github.com/JoanZapata/android-iconifyandroid
教程地址:http://blog.joanzapata.com/iconify-just-got-a-lot-better/github
demo地址:https://play.google.com/store/apps/details?id=com.joanzapata.android.icons.sampleapp
或者:https://github.com/JoanZapata/android-iconify/tree/master/android-iconify-sampleionic
iconify是一個矢量圖標庫,包含使用 Dave Gandy 製做的超過370中矢量字體圖標,可使Android應用開發者免於製做五種適用於不一樣屏幕大小尺寸的圖片,從而提升開發者工做效率。ide
優勢:因爲這些圖標均是矢量字體圖標,因此不只能夠無限放大而不會失真,模糊,並且能夠將適用於text的屬性應用於這些矢量圖標上,從而實現改變圖標顏色、添加陰影等效果佈局
缺點:目前在xml文件中使用圖標庫中的資源時,須要本身對照做者給出的sample查閱不一樣圖標所對應的標記,本身手敲標記,這樣不只麻煩,並且容易出錯字體
適用場景:gradle
iconify原做者提供了三種他自定義的控件:IconTextView、IconButton、IconToggleButton,能夠直接使用這三類控件來顯示iconify中提供的字體圖標;
在java代碼中經過使用一個IconDrawable爲具備setIcon(Drawable drawable)方法的控件設置該字體圖標
2、使用步驟(我使用的編譯器是Android Studio)
在須要使用iconify的app的build.gradle的dependencies中添加依賴(下面添加了整個庫,在實際開發過程當中,能夠只添加本身須要的某一個或幾個庫便可):
dependencies { compile 'com.joanzapata.iconify:android-iconify-fontawesome:2.2.1' // (v4.5) compile 'com.joanzapata.iconify:android-iconify-entypo:2.2.1' // (v3,2015) compile 'com.joanzapata.iconify:android-iconify-typicons:2.2.1' // (v2.0.7) compile 'com.joanzapata.iconify:android-iconify-material:2.2.1' // (v2.0.0) compile 'com.joanzapata.iconify:android-iconify-material-community:2.2.1' // (v1.4.57) compile 'com.joanzapata.iconify:android-iconify-meteocons:2.2.1' // (latest) compile 'com.joanzapata.iconify:android-iconify-weathericons:2.2.1' // (v2.0) compile 'com.joanzapata.iconify:android-iconify-simplelineicons:2.2.1' // (v1.0.0) compile 'com.joanzapata.iconify:android-iconify-ionicons:2.2.1' // (v2.0.1)}
自定義一個繼承自Application類的類:
import android.app.Application; import com.joanzapata.iconify.Iconify; import com.joanzapata.iconify.fonts.EntypoModule; import com.joanzapata.iconify.fonts.FontAwesomeModule; import com.joanzapata.iconify.fonts.IoniconsModule; import com.joanzapata.iconify.fonts.MaterialCommunityModule; import com.joanzapata.iconify.fonts.MaterialModule; import com.joanzapata.iconify.fonts.MeteoconsModule; import com.joanzapata.iconify.fonts.SimpleLineIconsModule; import com.joanzapata.iconify.fonts.TypiconsModule; import com.joanzapata.iconify.fonts.WeathericonsModule; 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()); } }
在manifest文件中的application中聲明使用自定義的MyApplication:
<application android:name="com.application.MyApplication" android:icon="@mipmap/ic_launcher" android:label="@string/app_name">
通過以上幾步就能夠在應用中使用iconify圖標了,如在佈局文件中的使用:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.activity.NewTaskActivity" android:gravity="center_horizontal"> <com.joanzapata.iconify.widget.IconTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/iconify_tv" android:text="{fa-heart @color/color_iconify_heart_pink 48sp} {fa-star #FFFF00 44sp} {fa-gift #FF00FF 48sp}" android:layout_marginTop="@dimen/activity_vertical_margin" android:layout_margin="@dimen/activity_horizontal_margin" android:gravity="center" /> <com.joanzapata.iconify.widget.IconButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/iconify_btn" android:layout_below="@+id/iconify_tv" android:layout_marginLeft="30dp" android:text="{entypo-facebook #008B00 35sp}"/> <com.joanzapata.iconify.widget.IconToggleButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/iconify_toggle" android:layout_below="@+id/iconify_btn" android:layout_marginLeft="30dp" android:textOff="{fa-lock}" android:textOn="{fa-unlock}" android:textSize="35sp" android:textColor="#677EFF"/> </RelativeLayout>
效果圖以下:
在代碼中使用:
@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); /* 設置menu的圖標爲iconify中的圖標*/ menu.findItem(R.id.action_settings).setIcon( new IconDrawable(this, FontAwesomeIcons.fa_user) .colorRes(R.color.color_iconify_music_quartz) .actionBarSize()); return true; }
效果圖以下:
問題:在使用ImageView.setImageIcon(Drawable drawable)時,圖標老是沒有顯示出來,不知道什麼緣由,還須要後面再看看