高效的字型載入
載入自定義字型可能會導致效能下降。我強烈建議使用這個小幫手將你已經使用的字型儲存/載入到 Hashtable 中。
public class TypefaceUtils {
private static final Hashtable<String, Typeface> sTypeFaces = new Hashtable<>();
/**
* Get typeface by filename from assets main directory
*
* @param context
* @param fileName the name of the font file in the asset main directory
* @return
*/
public static Typeface getTypeFace(final Context context, final String fileName) {
Typeface tempTypeface = sTypeFaces.get(fileName);
if (tempTypeface == null) {
tempTypeface = Typeface.createFromAsset(context.getAssets(), fileName);
sTypeFaces.put(fileName, tempTypeface);
}
return tempTypeface;
}
}
用法:
Typeface typeface = TypefaceUtils.getTypeface(context, "RobotoSlab-Bold.ttf");
setTypeface(typeface);