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

查看:71
本文介绍了自定义字体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;
    }
}

我正在XML属性为 customFont ="ArialRounded.ttf" 的XML文件中使用它,并且运行良好.

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

我正在用ArrayAdapter填充的ListView中使用此TextViewPlus.

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天全站免登陆