加载系统字体的文件管理器类
public class FontManager {
// This function enumerates all fonts on Android system and returns the HashMap with the font
// absolute file name as key, and the font literal name (embedded into the font) as value.
static public HashMap< String, String > enumerateFonts()
{
String[] fontdirs = { "/system/fonts", "/system/font", "/data/fonts" };
HashMap< String, String > fonts = new HashMap< String, String >();
TTFAnalyzer analyzer = new TTFAnalyzer();
for ( String fontdir : fontdirs )
{
File dir = new File( fontdir );
if ( !dir.exists() )
continue;
File[] files = dir.listFiles();
if ( files == null )
continue;
for ( File file : files )
{
String fontname = analyzer.getTtfFontName( file.getAbsolutePath() );
if ( fontname != null ) {
// Log.d("fonts", fontname+" : "+file.getAbsolutePath());
fonts.put(file.getAbsolutePath(), fontname);
}
}
}
return fonts.isEmpty() ? null : fonts;
}
字体详细信息模型类:
public class FontDetail
{
String sFontNames;
String sFontPaths;
String sFontType;
public FontDetail(String sFontNames, String sFontPaths, String sFontType)
{
this.sFontNames = sFontNames;
this.sFontPaths = sFontPaths;
this.sFontType = sFontType;
}
public String getsFontNames()
{
return sFontNames;
}
public void setsFontNames(String sFontNames)
{
this.sFontNames = sFontNames;
}
public String getsFontPaths()
{
return sFontPaths;
}
public void setsFontPaths(String sFontPaths)
{
this.sFontPaths = sFontPaths;
}
public String getsFontType()
{
return sFontType;
}
public void setsFontType(String sFontType) {
this.sFontType = sFontType;
}
}
数组列表以保存字体列表:
ArrayList arrLstFontDetail = new ArrayList();
在arrLstFontDetails中加载系统字体:
public void loadSystemFontsToListView() {
try {
Iterator> iter = FontManager.enumerateFonts().entrySet().iterator();
while (iter.hasNext()) {
@SuppressWarnings("rawtypes")
Map.Entry mEntry = (Map.Entry) iter.next();
LogMaintain.ShowLog(LogMaintain.LogType.Error, "System : " + (String) mEntry.getValue() + "Key: " + (String) mEntry.getKey());
arrLstFontDetail.add(new FontDetail((String) mEntry.getValue(), (String) mEntry.getKey(), "System"));
}
}