Created
October 6, 2015 08:42
-
-
Save harshvishu/f6c0445eae358b99e707 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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