运行时异常:无法创建本机字体或自定义 TextView 加载字体的内存泄漏



我的代码中存在一个巨大的问题,我正在从自定义TextView类加载assetsfonts文件夹中的字体。第一个问题是它在4.0设备上崩溃,但Caused by: java.lang.RuntimeException: native typeface cannot be made除外。我在这里对该方法使用相同的过程:

public class MyTextView extends TextView {
      public MyTextView(Context context, AttributeSet attrs, int defStyle) {
          super(context, attrs, defStyle);
      }
     public MyTextView(Context context, AttributeSet attrs) {
          super(context, attrs);
      }
     public MyTextView(Context context) {
          super(context);
     }

    public void setTypeface(Typeface tf, int style) {
        if (style == Typeface.BOLD) {
            super.setTypeface(Typeface.createFromAsset(
                    getContext().getAssets(), "fonts/hirakakupronbold.ttf"));
        } else if (style == Typeface.ITALIC) {
            super.setTypeface(Typeface.createFromAsset(
                    getContext().getAssets(), "fonts/hirakakupronitalic.ttf"));
        } else {
            super.setTypeface(Typeface.createFromAsset(
                    getContext().getAssets(), "fonts/hirakakupron.ttf"));
        }
    }
}

请注意,我正在使用扩展.ttf,并且我发现这导致了RunTimeException。所以我用.otf扩展名转换了相应的字体,现在它已经在 4.0 设备中运行,但基于此处存在内存泄漏。这里有解决方法,但我不知道如何使用/调用它。任何帮助都可以,谢谢。

好的,所以我终于认为,在TextView类中实例化一个TypeFace对象会导致每次实例化相同的TextView时产生如此大的负载。这导致我的应用程序滞后,最终导致OutOfMemoryException。因此,我所做的是创建一个不同的自定义TypeFace类,该类将从资产调用我的字体,以便它从TypeFace类而不是从TextView类实例化。

这是我的字体类:

public class TypeFaces {
    private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();
    public static Typeface getTypeFace(Context context, String assetPath) {
        synchronized (cache) {
            if (!cache.containsKey(assetPath)) {
                try {
                    Typeface typeFace = Typeface.createFromAsset(
                            context.getAssets(), assetPath);
                    cache.put(assetPath, typeFace);
                } catch (Exception e) {
                    Log.e("TypeFaces", "Typeface not loaded.");
                    return null;
                }
            }
            return cache.get(assetPath);
        }
    }
}

和自定义 TextView 类:

public class TextViewHirakaku extends TextView {
    public TextViewHirakaku(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    public TextViewHirakaku(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public TextViewHirakaku(Context context) {
        super(context);
    }
    public void setTypeface(Typeface tf, int style) {
        if (style == Typeface.BOLD) {
            super.setTypeface(TypeFaces.getTypeFace(getContext(),
                    "fonts/hirakakupronbold.ttf"));
        } else if (style == Typeface.ITALIC) {
            super.setTypeface(TypeFaces.getTypeFace(getContext(),
                    "fonts/hirakakupronitalic.ttf"));
        } else {
            super.setTypeface(TypeFaces.getTypeFace(getContext(),
                    "fonts/hirakakupron.ttf"));
        }
    }
}

请注意,我现在在这里从TypeFaces类调用getTypeFace方法。

如果您在

Android Studio上遇到此问题,请将您的资产放在主目录下,而不是将其放在res目录下。

同样在字体命名中仅使用小写字母和下划线,例如my_font.ttf

这对我来说就像魅力

如果要从 xml 扩展此视图,请尝试以这种方式使用它:

public class MyTextView extends TextView {
  public MyTextView(Context context, AttributeSet attrs, int defStyle) {
      super(context, attrs, defStyle);
      init();
  }
 public MyTextView(Context context, AttributeSet attrs) {
      super(context, attrs);
      init();
  }
 public MyTextView(Context context) {
      super(context);
      init();
 }

public void init() {
    Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/hirakakupronbold.ttf");
    setTypeface(tf);
}

}

它对我来说工作正常。为每个字体创建单独的类扩展文本视图 style.to 要应用它,请将"TextView"替换为"com.yourpackage.MyTextView"

问候

就我而言,我用于自定义视图 (costum) 的 XML 命名空间前缀设置不正确:

xmlns:costum="http://schemas.android.com/apk/tools"

我所要做的就是将其更改为

xmlns:costum="http://schemas.android.com/apk/res-auto"

它奏效了。

最新更新