Efficient Typeface loading
suggest changeLoading custom fonts can be lead to a bad performance. I highly recommend to use this little helper which saves/loads your already used fonts into a 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; }
}
Usage:
Typeface typeface = TypefaceUtils.getTypeface(context, "RobotoSlab-Bold.ttf"); setTypeface(typeface);
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents