RuntimeException的:原生字体不能作出或内存泄漏定制的TextView装载字体 [英] RuntimeException: native typeface cannot be made or memory leak for custom TextView loading font
问题描述
有一个在我的code一个巨大的问题,其中我从一个自定义的 TextView的<加载在我的
资产的字体\字体\
文件夹/ code>类。第一个问题是,它崩溃的4.0设备除外产生的原因:java.lang.RuntimeException的:本地字体不能言
。我用的是同样的过程<一href="http://alinberce.word$p$pss.com/2012/01/25/using-custom-fonts-in-android-bold-italic-regular-you-name-it/"相对=nofollow>这里与方法:
There's a HUGE problem in my code wherein I am loading a font in my assets\fonts\
folder from a custom TextView
class. The first problem is that it crashes on 4.0 devices with the exception Caused by: java.lang.RuntimeException: native typeface cannot be made
. I was using the same process here with the method:
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设备上运行,但有内存泄漏基础的此处。有变通办法这里,但我不知道如何使用/调用它。任何帮助会做,谢谢你。
Notice that I'm using the extension .ttf
, and I found that this is causing the RunTimeException
. So I converted the respective fonts with a .otf
extensions, and now it runs already in 4.0 devices but has memory leaks basing here. There are workarounds here but I don't know how to use/call it. Any help would do, thank you.
推荐答案
好了,我终于想通了实例内的一个字体
对象的TextView
类将同的TextView
被实例化,每次造成这么大的负荷。这引起了我的应用程序滞后,导致以 OutOfMemoryException异常
终。所以我所做的就是创建一个不同的自定义字体
,将打电话给我的字体从资产,使其从字体实例化类
类,而不是从的TextView
类。
Okay, so I finally figured that instantiating a TypeFace
object inside a TextView
class would cause so much load each time that same TextView
is instantiated. This caused my app to lag and resulted to OutOfMemoryException
eventually. So what I did was to create a different custom TypeFace
class that would call my fonts from the assets so that it instantiates from the TypeFace
class and not from the TextView
class.
下面是我的字体类:
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类:
And the custom TextView class:
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"));
}
}
}
请注意,我现在从字体
类调用 getTypeFace
方法在这里。
Notice that I'm now calling getTypeFace
method from TypeFaces
class here.
这篇关于RuntimeException的:原生字体不能作出或内存泄漏定制的TextView装载字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!