设置自定义字体后ListView变慢

6
我有一些数据在xml文件中,放在 /res/values/mydata.xml 中。我想用自定义字体显示数据在一个listview中。在模拟器中一切都很好,但是在真实设备上(使用安卓4.0.3的三星Galaxy Tab 10.1 2)滚动listview时太慢了。实际上,使用默认字体它可以很好地工作,但是当设置自定义字体时就出现问题。
这是我的Java代码:
public class ShowFoodCalorie extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // reading data from xml file
    setListAdapter(new MyAdapter(this, android.R.layout.simple_list_item_1,
            R.id.textView1,  getResources().getStringArray(R.array.food_cal)));
}
private class MyAdapter extends ArrayAdapter<String> {
    public MyAdapter(Context context, int resource, int textViewResourceId,
            String[] string) {
        super(context, resource, textViewResourceId, string);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.show_all, parent, false);
        String[] item = getResources().getStringArray(R.array.food_cal);
        TextView tv = (TextView) row.findViewById(R.id.textView1);
        try {
            Typeface font = Typeface.createFromAsset(getAssets(),"myFont.ttf");
            tv.setTypeface(font);
        } catch (Exception e) {
            Log.d("Alireza", e.getMessage().toString());
        }

        tv.setText(item[position]);
        return row;
    }
}

这是什么问题?它与我的设备有关吗?有什么解决方案可以帮助我吗? 谢谢。

1个回答

16

你的问题出在这一行:

Typeface font = Typeface.createFromAsset(getAssets(),"myFont.ttf");

你应该在Adapter的构造函数中执行一次此操作,将font设置为成员变量,然后只需使用该变量调用setTypeface(font)即可在TextView上设置字体。

应该避免在getView()方法中进行大量的加载操作。

另外了解一下convertView / ViewHolder模式对于Adapter来说也是有益的,可以提高性能。

示例更新如下:

private class MyAdapter extends ArrayAdapter<String> {
    Typeface font;

    public MyAdapter(Context context, int resource, int textViewResourceId,
            String[] string) {
        super(context, resource, textViewResourceId, string);
        font = Typeface.createFromAsset(context.getAssets(),"myFont.ttf");
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.show_all, parent, false);
        String[] item = getResources().getStringArray(R.array.food_cal);
        TextView tv = (TextView) row.findViewById(R.id.textView1);
        try {
            tv.setTypeface(font);
        } catch (Exception e) {
            Log.d("Alireza", e.getMessage().toString());
        }

        tv.setText(item[position]);
        return row;
    }
}

1
以下链接提供了@WarrenFaith建议的convertView/ViewHolder模式的有关信息。 http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/ - MemoryLeak
你能提供一个例子来更新一下吗?这样其他人也可以从中受益 :) - sanjeev
@sanjeev 完成了!虽然我不建议使用这个旧代码,而是改用RecyclerView。 - WarrenFaith
@WarrenFaith,实际上我正在使用RecyclerView,但不幸的是,当我从菜单中调用该类时,应用程序会卡住并需要一些时间才能打开活动。当我在寻找解决方案时遇到了这个问题 :) - sanjeev
在布局中使用android:fontFamily设置字体会降低性能吗?注意:我正在使用可下载的Google字体。 - sanjeev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接