3
所以我已经扩展到TextView
使用自定义字体(如描述here),即使用自定义的字体在正确的Android
public class CustomTextView extends TextView {
public static final int CUSTOM_TEXT_NORMAL = 1;
public static final int CUSTOM_TEXT_BOLD = 2;
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
initCustomTextView(context, attrs);
}
private void initCustomTextView(Context context, AttributeSet attrs) {
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CustomTextView, 0, 0);
int typeface = array.getInt(R.styleable.CustomTextView_typeface, CUSTOM_TEXT_NORMAL);
array.recycle();
setCustomTypeface(typeface);
}
public setCustomTypeface(int typeface) {
switch(typeface) {
case CUSTOM_TEXT_NORMAL:
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "customTextNormal.ttf");
setTypeface(tf);
break;
case CUSTOM_TEXT_BOLD:
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "customTextBold.ttf");
setTypeface(tf);
break;
}
}
}
我然后加入到活动的主要布局的片段使用CustomTextView
。所有的工作都很好,但似乎存在一些内存问题,即每次旋转屏幕(导致活动经历其生命周期)时,除了之前的加载之外,字体资产都会加载到本地堆中。例如;下面是从adb shell dumpsys meminfo my.package.com
屏幕转储初始加载后没有屏幕旋转(使用的Roboto-Light字体):
,并且在同一屏幕转储几个rotaions后
清楚的是在每个屏幕旋转上发生的资产分配和本地堆的增加(GC不会清除这一点)。当然,我们不应该以上述方式使用自定义字体,如果不是,我们应该如何使用自定义字体?
看起来不错,而且合理。 –