我正在使用.ttf字体在安卓系统中自定义一个TextView
,使用:
Typeface handType = Typeface.createFromAsset ( getContext().getAssets(), "fonts/JOURNAL.TTF");
问题是,当它进入编辑模式时,字符不会像默认的内置字体那样立即出现在屏幕上,但它们需要一段时间才能呈现,虽然简短,但足以让人感觉迟钝。
有没有什么技术(缓存等)可以帮助我即时渲染字体?
我还注意到,延迟因字体而异,并且似乎随着字体复杂性的增加而变得最差
发布于 2013-11-15 17:17:00
您可以尝试使用工厂。这真的更好,因为我们不会每次都分配字体。
import java.util.HashMap;
import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;
public class FontFactory {
private static FontFactory instance;
private HashMap<String, Typeface> fontMap = new HashMap<String, Typeface>();
private Context context;
private FontFactory(Context context) {
this.context = context.getApplicationContext();
}
public static FontFactory getInstance(Context context) {
if(instance == null){
return instance = new FontFactory(context);
} else {
return instance;
}
}
public Typeface getFont(String font) {
Typeface typeface = fontMap.get(font);
if (typeface == null) {
try {
typeface = Typeface.createFromAsset(context.getResources().getAssets(), "fonts/" + font);
fontMap.put(font, typeface);
} catch (Exception e) {
Log.e("FontFactory", "Could not get typeface: " + e.getMessage() + " with name: " + font);
return null;
}
}
return typeface;
}
}
https://stackoverflow.com/questions/8358162
复制相似问题