内存泄漏的自定义字体设置自定义字体 [英] Memory leaks with custom font for set custom font

查看:126
本文介绍了内存泄漏的自定义字体设置自定义字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面code设置自定义字体减慢我的整个应用程序。我怎么修改,以避免内存泄漏,增加的速度和内存管理呢?

 公共类FontTextView扩展TextView的{
    私有静态最后字符串变量=FontTextView;

    公共FontTextView(上下文的背景下){
        超(上下文);
    }

    公共FontTextView(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
        setCustomFont(背景下,ATTRS);
    }

    公共FontTextView(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
        超(背景下,ATTRS,defStyle);
        setCustomFont(背景下,ATTRS);
    }

    私人无效setCustomFont(上下文CTX,AttributeSet中的ATTRS){
        TypedArray A = ctx.obtainStyledAttributes(ATTRS,R.styleable.FontTextView);
        字符串CustomFont的= a.getString(R.styleable.FontTextView_customFont);
        setCustomFont(CTX时,CustomFont);
        a.recycle();
    }

    公共布尔setCustomFont(上下文CTX,串资产){
        字样TF = NULL;
        尝试 {
        TF = Typeface.createFromAsset(ctx.getAssets(),字体/+资产);
        }赶上(例外五){
            Log.e(TAG,无法获取字体:+ e.getMessage());
            返回false;
        }

        setTypeface(TF);
        返回true;
    }
    }
 

解决方案

您应该缓存的字样,否则的你可能会冒险在旧手机的内存泄漏。高速缓存会提高速度,以及因为它不是超快速的资产来读取所有的时间。

 公共类FontCache {

    私有静态哈希表<字符串,字体> fontCache =新的Hashtable<字符串,字体>();

    公共静态字样的get(字符串名称,上下文语境){
        字样TF = fontCache.get(名称);
        如果(TF == NULL){
            尝试 {
                TF = Typeface.createFromAsset(context.getAssets(),名);
            }
            赶上(例外五){
                返回null;
            }
            fontCache.put(姓名,TF);
        }
        返回TF;
    }
}
 

我给了一个如何加载自定义字体和样式textviews作为回答类似的问题的完整的例子。你似乎做的大部分是正确的,但你应该缓存的字体上面推荐的。

The following code for setting custom fonts slows down my whole app. how do i modify it to avoid memory leaks and increase the speed and manage memory well?

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

解决方案

You should cache the TypeFace, otherwise you might risk memory leaks on older handsets. Caching will increase speed as well since it's not super fast to read from assets all the time.

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

I gave a full example on how to load custom fonts and style textviews as an answer to a similar question. You seem to be doing most of it right, but you should cache the font as recommended above.

这篇关于内存泄漏的自定义字体设置自定义字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆