用于设置自定义字体的自定义字体的内存泄漏

     2023-02-23     201

关键词:

【中文标题】用于设置自定义字体的自定义字体的内存泄漏【英文标题】:Memory leaks with custom font for set custom font 【发布时间】:2013-05-29 21:50:51 【问题描述】:

以下用于设置自定义字体的代码会降低我的整个应用程序的速度。如何修改它以避免内存泄漏并提高速度和管理内存?

public class FontTextView extends TextView 
    private static final String TAG = "FontTextView";

    public FontTextView(Context context) 
        super(context);
    

    public FontTextView(Context context, AttributeSet attrs) 
        super(context, attrs);
        setCustomFont(context, attrs);
    

    public FontTextView(Context context, AttributeSet attrs, int defStyle) 
        super(context, attrs, defStyle);
        setCustomFont(context, attrs);
    

    private void setCustomFont(Context ctx, AttributeSet attrs) 
        TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.FontTextView);
        String customFont = a.getString(R.styleable.FontTextView_customFont);
        setCustomFont(ctx, customFont);
        a.recycle();
    

    public boolean setCustomFont(Context ctx, String asset) 
        Typeface tf = null;
        try 
        tf = Typeface.createFromAsset(ctx.getAssets(),"fonts/"+ asset);  
         catch (Exception e) 
            Log.e(TAG, "Could not get typeface: "+e.getMessage());
            return false;
        

        setTypeface(tf);  
        return true;
    
    

【问题讨论】:

【参考方案1】:

你应该缓存字体,否则you might risk memory leaks on older handsets。缓存也会提高速度,因为从资源中读取并不是非常快。

public class FontCache 

    private static Hashtable<String, Typeface> fontCache = new Hashtable<String, Typeface>();

    public static Typeface get(String name, Context context) 
        Typeface tf = fontCache.get(name);
        if(tf == null) 
            try 
                tf = Typeface.createFromAsset(context.getAssets(), name);
            
            catch (Exception e) 
                return null;
            
            fontCache.put(name, tf);
        
        return tf;
    

我给了full example on how to load custom fonts and style textviews as an answer to a similar question。您似乎大部分都做对了,但您应该按照上面的建议缓存字体。

【讨论】:

如何在 setcustomfont 的 nmy 代码中调用 fontcache?我每次尝试都做不好 替换 tf = Typeface.createFromAsset(ctx.getAssets(),"fonts/"+asset); with tf = FontCache.get("fonts/" + assets, ctx); 有理由使用Hashtable吗?如果没有,ArrayMap 可能是更好的类型,因为内存消耗和迭代速度更低。 其实我并没有过多考虑数据结构的选择。 ArrayMap 可能工作得同样好或更好! 我不确定是否总是这样,但createFromAsset() 方法已经使用动态LruCache 来存储每个Typeface。这种方法通过创建第二个缓存来浪费资​​源。【参考方案2】:

我觉得不需要使用字体缓存。我们可以这样做吗?

对以上代码稍作改动,如有错误请指正。

public class FontTextView extends TextView 
    private static final String TAG = "FontTextView";
    private static Typeface mTypeface;

    public FontTextView(Context context) 
        super(context);
    

    public FontTextView(Context context, AttributeSet attrs) 
        super(context, attrs);
    

    public FontTextView(Context context, AttributeSet attrs, int defStyle) 
        super(context, attrs, defStyle);
        if (mTypeface == null) 
            mTypeface = Typeface.createFromAsset(context.getAssets(),   GlobalConstants.SECONDARY_TTF);
        
        setTypeface(mTypeface);
    

    

【讨论】:

如果你只有一个字体,这个解决方案可以工作。其他解决方案更适合多种字体情况。 是的,但我认为不建议为单个 android 应用程序使用更多字体。? 我从未听说过这样的事情。如果您有资源,我想阅读

iPhone中的自定义字体

...我想知道如何在iphone上使用自定义TTF或OTF字体,例如一种用于电话板的字体类型,另一种用于消息等...可以帮助我吗?【问题讨论】:您是否计划开发应用程序,或者您是否计划开发任何应用程序通常都可以使用的主题以及iPhone... 查看详情

用于重用的自定义 UIButton 类

】用于重用的自定义UIButton类【英文标题】:CustomUIButtonClassforReuse【发布时间】:2015-05-3017:56:54【问题描述】:在网上找了一段时间了,还没有得到关于如何做到这一点的直接答案。在我的应用程序中,我需要一个驻留在许多View... 查看详情

ios:自定义字体的字距调整

...ton的自定义字体设置字距吗?以下方法无效。字距调整适用于系统字体,但不适用于自定义字体。有人可以帮忙解决问题吗?NSMutableAttributedString*attributedString=[[NSMutableAttribut 查看详情

显示 iPhone 泄漏的自定义单元格代码

...创建一个自定义单元格。当我测试应用程序时,仪器显示用于创建自定义单元格的代码存在泄漏。我没有不明白我必须在哪里释放用于消除泄漏的东西。谁能帮我解决这个问题。这是我用于创建自定义单元格的代码。-( 查看详情

Xcode 6 大小类中的自定义字体大小无法与自定义字体一起正常工作

...ton中的字体和字体大小根据当前设备配置的大小等级自动设置,就在情节提要中。例如 查看详情

Android API中的自定义字体低于16?

...我读过这个:Android4.1添加了更多Roboto字体样式的变体,用于共有10个变体,它们都可以被应用程序使用。您现在的应用可以访问全套轻量级和浓缩型变体。(这里:link)我也在某处读到,我 查看详情

NavigationDrawer 上的自定义字体

】NavigationDrawer上的自定义字体【英文标题】:CustomfontonNavigationDrawer【发布时间】:2015-10-2615:46:46【问题描述】:我正在尝试更改我的导航抽屉上的字体,我正在使用带有适配器的自定义xml文件,因此我可以成功更改ListView中每... 查看详情

UIbarbuttonItem 标题的自定义字体

】UIbarbuttonItem标题的自定义字体【英文标题】:CustomfonttoaUIbarbuttonItemtitle【发布时间】:2011-03-0106:13:32【问题描述】:我需要更改UIBarButtonItem标题的字体。目前是barbuttonItem.customView=customLabel。现在我想添加一个选择器和目标,这... 查看详情

界面生成器中具有动态类型的自定义字体

...】:我的Storyboard中有几个视图,它们有UILabels。这些标签设置为AutomaticallyAdjustsFont,并使用Body或Headline等文本样式。我知道有这样一种程序化方法:https://useyourloaf. 查看详情

lwuit中表格的自定义字体?

】lwuit中表格的自定义字体?【英文标题】:Customfontfortableinlwuit?【发布时间】:2012-05-2107:20:25【问题描述】:在我的应用程序中,我使用资源文件为表格组件使用了自定义字体。我在资源文件中创建了一种字体Font.size=10Font.Face=T... 查看详情

Xcode Playground 中的自定义字体

】XcodePlayground中的自定义字体【英文标题】:CustomfontsinXcodeplayground【发布时间】:2014-12-1418:41:48【问题描述】:我正在用代码编写界面布局。因为每次我测试字体大小或逐像素查看布局时重新加载应用程序很烦人,所以我开始... 查看详情

DOMPDF 的自定义字体

】DOMPDF的自定义字体【英文标题】:CustomFontsforDOMPDF【发布时间】:2012-09-1620:45:03【问题描述】:我正在使用DOMPDF0.6.0Beta2。我想在PDF文件中使用自定义字体(字体:\'SegeoPrint\'、\'LucidaHandwriting\'、\'AirplanesintheNightSky\')。我按照... 查看详情

Interface Builder 中的自定义字体

】InterfaceBuilder中的自定义字体【英文标题】:CustomfontsinInterfaceBuilder【发布时间】:2014-03-2823:05:31【问题描述】:现在是2014年,有没有办法在InterfaceBuilder中设置自定义字体?即不要以编程方式进行,除了应用程序列表中的“应... 查看详情

从 node_modules 到 angular-cli 的自定义字体

...hub问题,但找不到适合我的东西。我有一个自定义字体,用于通过npm安装的图标。这些是我的文件夹a-iconsnode_modules中的文件:a-icon.cssa-icon.eot, 查看详情

Pixi.js 中的自定义字体

】Pixi.js中的自定义字体【英文标题】:CustomfontinPixi.js【发布时间】:2013-09-3022:09:03【问题描述】:我尝试将自定义字体加载到Pixi.js(2DWebGL框架)中。他们有一个使用.woffgoogle字体的例子:https://github.com/GoodBoyDigital/pixi.js/tree/mast... 查看详情

css添加带权重的自定义字体(代码片段)

查看详情

css添加带权重的自定义字体(代码片段)

查看详情

Xamarin 表单:UWP 和 Windows 8.1 中的自定义字体

...windows10和windows8.1上运行。我需要为我的标签创建样式以设置字体系列。对于android和ios,我引用了 查看详情