Created
December 30, 2017 10:20
-
-
Save abhinav272/1f7a4b01ade6f222e886eec01255b4a2 to your computer and use it in GitHub Desktop.
FontCache for getting typeface from assets. Loading typeface every time is little expensive.
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
import android.content.Context; | |
import android.graphics.Typeface; | |
import java.util.HashMap; | |
import java.util.Map; | |
/** | |
* Created by abhinav.sharma on 28/12/16. | |
* Creating Typeface from assets is little expensive when lot of textviews wants custom Fonts from assets | |
* https://nayaneshguptetechstuff.wordpress.com/2014/06/20/slow-activity-transition-using-custom-typeface-font/ | |
*/ | |
public class FontCache { | |
private static Map<String, Typeface> fontCache = new HashMap<>(); | |
public static synchronized Typeface getTypeface(String fontname, Context context) { | |
Typeface typeface = fontCache.get(fontname); | |
if (typeface == null) { | |
try { | |
typeface = Typeface.createFromAsset(context.getAssets(), String.format("fonts/%s", fontname)); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
return null; | |
} | |
fontCache.put(fontname, typeface); | |
} | |
return typeface; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment