@@ -19,6 +19,9 @@ public class CapitalizedTextView extends Button {
19
19
private static final boolean SANS_ICE_CREAM = Build .VERSION .SDK_INT < Build .VERSION_CODES .ICE_CREAM_SANDWICH ;
20
20
private static final boolean IS_GINGERBREAD = Build .VERSION .SDK_INT >= Build .VERSION_CODES .GINGERBREAD ;
21
21
22
+ private static final String TAG = "Typefaces" ;
23
+ private static final Hashtable <String , Typeface > cache = new Hashtable <String , Typeface >();
24
+
22
25
public CapitalizedTextView (Context context ) {
23
26
super ( context );
24
27
@@ -52,9 +55,25 @@ public void setText(CharSequence text, BufferType type) {
52
55
}
53
56
}
54
57
55
- private void setTF (Context context ) {
56
- setTypeface ( Typeface .createFromAsset (context .getAssets (), "fonts/Roboto-Regular.ttf" ) );
58
+ public static Typeface getTypeFace (Context c , String assetPath ) {
59
+ synchronized (cache ) {
60
+ if (!cache .containsKey (assetPath )) {
61
+ try {
62
+ Typeface t = Typeface .createFromAsset (c .getAssets (),
63
+ assetPath );
64
+ cache .put (assetPath , t );
65
+ } catch (Exception e ) {
66
+ Log .e (TAG , "Could not get typeface '" + assetPath
67
+ + "' because " + e .getMessage ());
68
+ return null ;
69
+ }
70
+ }
71
+ return cache .get (assetPath );
72
+ }
57
73
}
58
74
59
-
75
+ private void setTF (Context context ) {
76
+ Typeface tf = getTypeFace (context , "fonts/Roboto-Regular.ttf" );
77
+ setTypeface ( tf );
78
+ }
60
79
}
0 commit comments