应用程序不断“从字体缓存中清除”最终由于内存不足而崩溃,android

发布于 2024-10-21 06:13:19 字数 152 浏览 1 评论 0原文

我刚刚开始构建一个应用程序(它甚至还没有执行任何操作,但显示了一些按钮),当我运行它时,我在 logcat 中收到错误消息: “从字体缓存中获取 193K [23 项]” 一遍又一遍,直到大约一分钟后,应用程序因内存不足而崩溃。我的 3 个按钮是自定义按钮,使用自定义字体。也许字体有问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最近可好 2024-10-28 06:13:19

问题是调用 Typeface.createFromAsset()。

我减少了创建字体工厂的次数,因此每个字体类型都会调用一次 Typeface.createFromAsset() 。

字体工厂将字体保存在哈希图中,这就达到了目的。

我在此链接上找到了解决方案并进行了一些调整:

http://www.levinotik.com/2011/09/22/custom-fonts-in-android-can-cause-issues-heres-how-to -fix-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;
    }
}

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;
    }
}
丑丑阿 2024-10-28 06:13:19

通过在活动类中将 Typeface 声明为静态,我已经能够大幅减少这种skia 消息(以及最终的低内存情况)。

即,

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");
    }
}

这可能看起来有风险,但至少用户可以在我的两个活动之间来回切换,而不会进入低内存状态!

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 !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文