I'm right at the beginning of building an app (which doesn't even do anything yet, but display some buttons) and when I run开发者_运维知识库 it, I get the error message in logcat: "purding 193K from font cache [23 entries]" over and over, until about a minute later the app crashes due to low memory. My 3 buttons are custom buttons, using a custom font. Problem with the font perhaps?
The problem is calling Typeface.createFromAsset().
I've reduced that creating a font factory, so it calls Typeface.createFromAsset() once per font type.
The font factory holds the typeface in a hashmap and that does the trick.
I found the solution on this link and tweaked a little bit:
http://www.levinotik.com/2011/09/22/custom-fonts-in-android-can-cause-issues-heres-how-to-fix-it/
This is how I've implemented it.
public class FontFactory {
private static FontFactory instance = new FontFactory();
private HashMap<String, Typeface> fontMap = new HashMap<String, Typeface>();
private FontFactory() {
}
public static FontFactory getInstance() {
return instance;
}
public Typeface getFont(String font) {
Typeface typeface = fontMap.get(font);
if (typeface == null) {
typeface = Typeface.createFromAsset(MyApplication.getApplicationContext().getResources().getAssets(), "fonts/" + font);
fontMap.put(font, typeface);
}
return typeface;
}
}
I've been able to drastically reduce this skia message (and eventual low-memory condition) by declaring the Typeface as static within the activity class.
i.e.
public class myActivity extends Activity
{
//font
private static Typeface mFontHelvet;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
if (mFontHelvet == null)
{
mFontHelvet = Typeface.createFromAsset(this.getAssets(), "Helvetica.TTF");
}
myTextView = (TextView) findViewById(R.id.mytextview);
myTextView.setTypeface(mFontHelvet);
myTextView.setText("blah blah");
}
}
this might seem risky, but at least a user can go back and forth between my two activities without entering a low-memory state !
精彩评论