运行android冰淇淋三明治的应用程序,在字体文件夹中导入roboto.ttf和roboto-bold.ttf字体后,在使用这些字体设置四个textview后,在滚动listview时非常(非常)慢。 有谁知道优化表演的方法?是否有提高速度的提示和技巧?
我澄清说在插入这些代码行之前它非常顺利:
Typeface roboto = Typeface.createFromAsset(activity.getAssets(), "fonts/Roboto-Regular.ttf");
Typeface robotobold = Typeface.createFromAsset(activity.getAssets(), "fonts/Roboto-Bold.ttf");
nome.setTypeface(robotobold);
mq.setTypeface(roboto);
citta.setTypeface(roboto);
prezzo.setTypeface(roboto);
descrizione.setTypeface(roboto);
我添加了可以帮助进行字体缓存的类:
public class TypefaceCache {
private final HashMap<String, Typeface> map;
private Context con;
public TypefaceCache(Context con) {
map = new HashMap<String, Typeface>();
this.con = con;
}
public Typeface getTypeface(String file) {
Typeface result = map.get(file);
if (result == null) {
result = Typeface.createFromAsset(con.getAssets(), file);
map.put(file, result);
}
return result;
}
}
我通过
调用类和字体 TypefaceCache typecache = new TypefaceCache(activity);
Typeface roboto = typecache.getTypeface("fonts/Roboto-Regular.ttf");
但结果是一样的......
答案 0 :(得分:4)
您可以尝试缓存Typeface
。
public class TypefaceCache {
private final HashMap<String, Typeface> map =
new HashMap<String, Typeface>();
private Typeface getTypeface(String file, Context context) {
Typeface result = map.get(file);
if (result == null) {
result = Typeface.createFromAsset(context.getAssets(), file);
map.put(file, result);
}
return result;
}
}
答案 1 :(得分:1)
我不久前遇到过这个问题,解决办法是将类设置为静态,就像在blog中描述的那样;
public class TypefaceSingleton {
private static TypefaceSingleton instance = new TypefaceSingleton();
private TypefaceSingleton() {}
public static TypefaceSingleton getInstance() {
return instance;
}
public Typeface getTypeface() {
return Typeface.createFromAsset(ThinkNearApp.getContext().getResources().getAssets(), "fonts/Roboto-Bold.ttf");
}
}
答案 2 :(得分:0)
这是我的解决方案: 在asynctask中我启动ui我插入类变量
private final HashMap<String, Typeface> map;
然后我通过类构造函数
初始化它map = new HashMap<String, Typeface>();
我实现了在类中加载我的字体的方法:
private Typeface getTypeface(String file, Context context) {
Typeface result = map.get(file);
if (result == null) {
result = Typeface.createFromAsset(context.getAssets(), file);
map.put(file, result);
}
return result;
}
然后,每当我想要在asynctask中,我添加我喜欢的字体,我加载在我的textviews上,一切都很顺利。 谢谢大家