[bug]Android动态切换字体过程中内存占用不断增加的解决办法

按照如下原DMS文档(现已更新),实现了切换字体功能
Font Install and Runtime Change On ICS guideline.doc

按照参考文档完成此feature之后,发现不断在不同字体之间切换,手机内存占用会不断增加
这是因为typeface的createFromAsset接口有BUG,每调用一次该接口,native memory占用就会增加,这个问题属于Google Bug,目前尚无完美的解决方案,但是可以使用下面的方法work around.

[SOLUTION]
1.修改framework中的typeface.java文件
增加以下代码:
import java.util.Hashtable;
private static final String TAG = “Typefaces”;
private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();
public static Typeface get(AssetManager mgr, String assetPath) {
synchronized (cache) {
if (!cache.containsKey(assetPath)) {
try {
Typeface t = Typeface.createFromAsset(mgr, assetPath);
cache.put(assetPath, t);
} catch (Exception e) {
return null;
}
}
return cache.get(assetPath);
}
}

2.修改Textview.java,
将Typeface.createFromAsset(…)替换成新加的Typeface.get(…)接口
根据我司提供的文档,需要修改的地方有2处,分别在textview构造函数,与setTextAppearance函数中.

猜你喜欢

转载自blog.csdn.net/zhangqi6627/article/details/107923115