Skip to content

Instantly share code, notes, and snippets.

@AbeHaruhiko
Last active August 29, 2015 14:00
Show Gist options
  • Save AbeHaruhiko/11083133 to your computer and use it in GitHub Desktop.
Save AbeHaruhiko/11083133 to your computer and use it in GitHub Desktop.
Androidで画像を使わずにオシャレなアイコンを簡単に表示する方法(Font Awesome) ref: http://qiita.com/AbeHaruhiko/items/a7437027231a373acf2a
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomFontButton">
<attr name="customFont" format="string"/>
</declare-styleable>
</resources>
import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;
import java.util.Hashtable;
public class CachedTypefaces {
private static final String TAG = "CachedTypefaces";
private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();
public static Typeface get(Context context, String assetPath) {
synchronized (cache) {
if (!cache.containsKey(assetPath)) {
try {
Typeface t = Typeface.createFromAsset(context.getAssets(), assetPath);
cache.put(assetPath, t);
} catch (Exception e) {
Log.e(TAG, "Could not get typeface '" + assetPath + "' because " + e.getMessage());
return null;
}
}
return cache.get(assetPath);
}
}
}
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;
import jp.caliconography.android.util.CachedTypefaces;
public class CustomFontButton extends Button {
private static final String TAG = "CustomFontButton";
public CustomFontButton(Context context) {
super(context);
}
public CustomFontButton(Context context, AttributeSet attrs) {
super(context, attrs);
setCustomFont(context, attrs);
}
public CustomFontButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setCustomFont(context, attrs);
}
private void setCustomFont(Context ctx, AttributeSet attrs) {
TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.CustomFontTextView);
String customFont = a.getString(R.styleable.CustomFontTextView_customFont);
setCustomFont(ctx, customFont);
a.recycle();
}
public boolean setCustomFont(Context context, String asset) {
Typeface tf = null;
try {
// ここでフォントファイル読み込み。
// 読み込み済みならキャッシュから。
tf = CachedTypefaces.get(context, asset);
} catch (Exception e) {
Log.e(TAG, "Could not get typeface: " + e.getMessage());
return false;
}
setTypeface(tf);
return true;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto"
<!-- ↑xmlnsの追加を忘れずに! -->
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<your.app.package.CustomFontButton
android:id="@+id/btnSomething"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="&#xf040;"
android:textSize="30sp"
android:textColor="#666"
myapp:customFont="fontawesome-webfont.ttf"
android:onClick="onClickSomething"/>
</LinearLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment