在我的應用程序中,我需要處理多個字體文件。因此,而不是每次創建新實例,我實現了辛格爾頓得到字樣這樣的: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
,但正如我所說我需要處理多種字體。我應該有多種方法來獲取實例嗎? –