在项目中引入了新的字库,项目中也自定了一个TextView 和 EditView 使用这个新的字库。代码如下:
public class CustomCashDigitFontTextView extends android.support.v7.widget.AppCompatTextView {
public CustomCashDigitFontTextView(Context context) {
super(context);
init(context);
}
public CustomCashDigitFontTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public CustomCashDigitFontTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
public void init(Context context) {
Typeface newFont = Typeface.createTypeface(context.getAssets(), "DIN-Medium-Number.ttf");
setTypeface(newFont);
}
}
我用下面的命令查看memory状况的时候发现:
adb shell dumpsys meminfo
内存的状况如下:
Asset Allocations
zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
zip:/data/app/com.sankuai.poscashier-2/base.apk:/assets/DIN-Medium-Number.ttf: 23K
asserts部分同样的字体库,内存被分配了多次。
主要是下面的代码引起的:
Typeface newFont = Typeface.createFromAsset(context.getAssets(), "DIN-Medium-Number.ttf");
每一次调用Typeface.createFromAsset,都会分配一次内存,导致同样的内容内存反复分配。
我有观察了最新的Android 源码,在最新的Android7.0的代码中,这个问题已经解决了。但是在Android 5.0+的版本上,问题依然存在。
方案很简单,引入一个缓存,当时同样的字体,不再分配新的内存。
public class TypefaceHelper {
private static final LruCache sDynamicTypefaceCache = new LruCache<>(8);
public static Typeface createTypeface(AssetManager assetManager, String path) {
synchronized (sDynamicTypefaceCache) {
Typeface typeFace = sDynamicTypefaceCache.get(path);
if (typeFace!=null) {
return typeFace;
} else {
typeFace = Typeface.createFromAsset(assetManager, path);
sDynamicTypefaceCache.put(path, typeFace);
return typeFace;
}
}
}
}
注意,要加上synchronized (sDynamicTypefaceCache)
线程同步锁,否则,在某些线程同步的情况下,也可能导致偶先的多次分配。