Android:Change View font and avoid memory leak
public static final int ROBOTO_REGULAR=1;
public static final int ROBOTO_THIN=2;
public static final int ROBOTO_THIN_ITALIC=3;
public static final int ROBOTO_LIGHT=4;
public static final int ROBOTO_BOLD=5;
public static final int ADOBE_CLEAN_LIGHT=6;
public static final int ADOBE_CLEAN_REGULAR=7;
public static final int ADOBE_CLEAN_BOLD = 8;
public static final int ADOBE_CLEAN_BOLD_ITALIC = 9;
@SuppressLint("UseSparseArrays")
// use a hashmap to avoid memory leak.
//Typeface.createFromAsset is the reason why memory leak
private static HashMap<Integer, Typeface> fontCache=new HashMap<Integer, Typeface>();
public static Typeface getTypeface(View v,int type)
{
if (fontCache.containsKey(type))
return fontCache.get(type);
else
{
Typeface tf = Typeface.createFromAsset(v.getContext().getAssets(),
"fonts/"+getFontByCode(type));
fontCache.put(type, tf);
return tf;
}
}
/**
* Change the whole view's text font
* @param root the root of view group
* @param act the view's activity
*/
public static void changeFonts(ViewGroup root, Activity act,int type) {
// Typeface tf = Typeface.createFromAsset(act.getAssets(),
// "fonts/"+getFontByCode(type));
for (int i = 0; i < root.getChildCount(); i++) {
View v = root.getChildAt(i);
Typeface tf=getTypeface(v, type);
if (v instanceof TextView) {
((TextView) v).setTypeface(tf);
} else if (v instanceof Button) {
((Button) v).setTypeface(tf);
} else if (v instanceof EditText) {
((EditText) v).setTypeface(tf);
} else if (v instanceof ViewGroup) {
changeFonts((ViewGroup) v, act,type);
}
}
}
/**
* Change a single view's text font
* @param v view
* @param type font type
*/
public static void changeViewFont(View v,int type)
{
Typeface tf=getTypeface(v, type);
if (v instanceof TextView) {
((TextView) v).setTypeface(tf);
} else if (v instanceof Button) {
((Button) v).setTypeface(tf);
} else if (v instanceof EditText) {
((EditText) v).setTypeface(tf);
}
}
You must Sign up as a member of Effecthub to view the content.
A PHP Error was encountered
Severity: Notice
Message: Undefined index: HTTP_ACCEPT_LANGUAGE
Filename: helpers/time_helper.php
Line Number: 22
Latest Posts
- Android: CookieManager removeAllCookie() Crash
- XlistView: A listview which can pull to refresh and load more (1)
- Millions of Android Phones Could Be Affected by the Heartbleed Bug. Check to See if Yours Is One of Them
- TOP 10 ANDROID GAMES FOR MARCH 2014
- Android:Button with different textcolor and background in different states
2827 views 0 comments
You must Sign up as a member of Effecthub to join the conversation.