自定义字体 TextView 的性能问题 [英] Performance issue on custom font TextView

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

问题描述

我有一个自定义的 TextView,带有个性化的字体属性:

I have a custom TextView, with a personalized font attribute:

public class TextViewPlus extends TextView {
    private static final String TAG = "TextViewPlus";
    public TextViewPlus(Context context) {
        super(context);
    }
    public TextViewPlus(Context context, AttributeSet attrs) {
        // This is called all the time I scroll my ListView
        // and it make it very slow. 
        super(context, attrs);
        setCustomFont(context, attrs);
    }
    public TextViewPlus(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.TextViewPlus);
        String customFont = a.getString(R.styleable.TextViewPlus_customFont);
        setCustomFont(ctx, customFont);
        a.recycle();
    }
    public boolean setCustomFont(Context ctx, String asset) {
        Typeface tf = null;
        try {
            tf = Typeface.createFromAsset(ctx.getAssets(), asset);  
            setTypeface(tf); 
        } catch (Exception e) {
            Log.e(TAG, "Could not get typeface: "+e.getMessage());
            return false;
        }
        return true;
    }
}

我在具有 customFont="ArialRounded.ttf" 属性的 XML 文件中使用它,它运行良好.

I'm using it in my XML files with the attribute customFont="ArialRounded.ttf", and it is working quite well.

我在 ListView 中使用这个 TextViewPlus,填充了 ArrayAdapter.

I'm using this TextViewPlus in a ListView, populated with an ArrayAdapter.

TextViewPlus dataText = (TextViewPlus) itemView.findViewById(R.id.data_text);
dataText.setText("My data String");

我的问题是,当我滚动 ListView 时,性能很糟糕!非常缓慢且充满滞后.TextViewPlus 构造函数 n°2 在我滚动列表时一直被调用.

My problem is that the performance, when I'm scrolling the ListView, are terrible! Very slow and full of lags. The TextViewPlus constructor n°2 it's called all the time i scroll the list.

如果我在普通的 TextView 中更改 TextViewPlus,并使用 dataText.setTypeface(myFont),一切都会顺利并且运行良好.

If I change TextViewPlus in a normal TextView, and use dataText.setTypeface(myFont), everything is smood and is working well.

如何在没有性能问题的情况下使用 TextViewPlus?

How can I use my TextViewPlus without performance issue?

推荐答案

为什么不把创建的 typface 对象保存在内存中,这样就不用在每次文本视图获取时都创建已创建.

Why don't you keep the created typface object in memory so that you don't create every time the text view is getting created.

以下是创建和缓存字体对象的示例类:

Following is a sample class that creates and cache the typeface object:

public class TypeFaceProvider {

    public static final String TYPEFACE_FOLDER = "fonts";
    public static final String TYPEFACE_EXTENSION = ".ttf";

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

    public static Typeface getTypeFace(Context context, String fileName) {
    Typeface tempTypeface = sTypeFaces.get(fileName);

    if (tempTypeface == null) {
        String fontPath = new StringBuilder(TYPEFACE_FOLDER).append('/').append(fileName).append(TYPEFACE_EXTENSION).toString();
        tempTypeface = Typeface.createFromAsset(context.getAssets(), fontPath);
        sTypeFaces.put(fileName, tempTypeface);
    }

    return tempTypeface;
    }
}

这篇关于自定义字体 TextView 的性能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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