在我的应用程序中,我需要处理多个字体文件。因此,而不是每次创建新实例,我实现了辛格尔顿得到字样这样的:Android:处理多个字体文件 - 正确的方法
public class FontSingleton {
private static FontSingleton instance;
private static Typeface typeface;
private FontSingleton(){
//private constructor to avoid direct creation of objects
}
public static FontSingleton getInstance(Context context,String fontFileName){
if(instance==null){
instance = new FontSingleton();
typeface = Typeface.createFromAsset(context.getResources().getAssets(), fontFileName);
}
return instance;
}
public Typeface getTypeFace(){
return typeface;
}
}
现在,我能得到typeface
这样的:
FontSingleton.getInstance(mContext,"font1.otf").getTypeFace();
是为应对正确的方法内存泄漏和实施Singleton?我是设计模式和Android的新手。任何人都可以引导我正确的方式吗?
谢谢,但“你不需要提供fontname,作为getInstance()的参数”。那我该怎么做呢? –
硬编码它直接在'createFromAsset' – Blackbelt
,但正如我所说我需要处理多种字体。我应该有多种方法来获取实例吗? –