以下是设置自定义字体的代码,它使我的整个应用程序变慢。我应该如何修改它以避免内存泄漏并提高速度和良好地管理内存?
public class FontTextView extends TextView {
private static final String TAG = "FontTextView";
public FontTextView(Context context) {
super(context);
}
public FontTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setCustomFont(context, attrs);
}
public FontTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setCustomFont(context, attrs);
}
private void setCustomFont(Context ctx, AttributeSet attrs) {
TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.FontTextView);
String customFont = a.getString(R.styleable.FontTextView_customFont);
setCustomFont(ctx, customFont);
a.recycle();
}
public boolean setCustomFont(Context ctx, String asset) {
Typeface tf = null;
try {
tf = Typeface.createFromAsset(ctx.getAssets(),"fonts/"+ asset);
} catch (Exception e) {
Log.e(TAG, "Could not get typeface: "+e.getMessage());
return false;
}
setTypeface(tf);
return true;
}
}
Hashtable
的原因吗?如果没有,由于内存消耗和迭代速度较低,ArrayMap
可能是更好的类型。 - wilkascreateFromAsset()
方法已经使用动态LruCache
来存储每个Typeface
。该方法通过创建第二个缓存浪费资源。 - BryanCalligraphy
库来处理字体要比这个解决方案好得多。 - blueware