Skip to content

Instantly share code, notes, and snippets.

@harshvishu
Created October 6, 2015 08:42
Show Gist options
  • Save harshvishu/f6c0445eae358b99e707 to your computer and use it in GitHub Desktop.
Save harshvishu/f6c0445eae358b99e707 to your computer and use it in GitHub Desktop.
/**
* Created by harsh on 04-Jul-15.
* <p/>
* class optimizes the memory operation to create Typeface which is costly
* creates a static reference as this method may be use several times during the code execution
*/
public class Typefaces {
private static final Hashtable<String, Typeface> cache = new Hashtable<>();
public static Typeface get(Context c, String name) {
synchronized (cache) {
if (!cache.containsKey(name)) {
Typeface t = Typeface.createFromAsset(
c.getAssets(),
String.format("fonts/%s.ttf", name)
);
cache.put(name, t);
}
return cache.get(name);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment