I'm always looking for ways to improve my Android dev skills... Hence this question.
Do you foresee any possible cons or pros about storing Typeface's statically for different fonts as they are being used by the app?
Pros:
Cons:
My "TypeFace" cacher:
public static synchronized Typeface getFontType (String fontFile)
{
if (!smTypefaces.contains(fontFile)) {
try {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/"+fontFile);
if (null != tf) {
// todo use maybe a weak reference?
smTypefaces.put(fontFile, tf);
}
} catch (RuntimeException e) {
e.printStackTrace();
}
}
return smTypefaces.get(fontFile);
}
I do exactly that, and I've never had a related memory issue. I created a class called FontProvider that lazy loads a static Map where the key is a string constant and the value is the instance of Typeface. I expose the map through a get(String key) method that returns null if the key isn't in the map.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With