【问题标题】:slow listview when set custom font设置自定义字体时列表视图慢
【发布时间】:2012-10-16 03:56:25
【问题描述】:

我在 xml 文件中有一些数据,将其放在 /res/values/mydata.xml 中。我想用自定义字体在列表视图中显示数据。模拟器中的一切都很棒,但在真实设备中(使用带有 android 4.0.3 的三星 Galaxy Tab 10.1 2)滚动列表视图时速度太慢。实际上它适用于默认字体,但设置自定义字体时会出现问题。

这是我的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;
    }
}

这是什么问题?是关于我的设备的吗?任何解决方案都可以帮助我。 谢谢

【问题讨论】:

    标签: java android android-layout truetype android-fonts


    【解决方案1】:

    你的问题是那行:

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

    您应该在适配器的构造函数中这样做一次,将 font 设为成员变量,而不仅仅是使用该变量在您的 TextView 上调用 setTypeface(font)。

    应防止getView() 方法中的重载。

    还可以阅读适配器的 convertView / ViewHolder 模式,这也可以提高性能。

    更新示例:

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

    【讨论】:

    • 下面的链接有关于 @WarrenFaith 建议的 convertView/ViewHolder 模式的好信息。 http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/
    • 你能更新一个例子吗?所以其他人可以从中受益:)
    • @sanjeev 完成!虽然我不鼓励使用这个旧代码并使用 recyclerview 来代替
    • @WarrenFaith 实际上我正在使用 recyclerview,不幸的是,当我从菜单中调用该类时,该应用程序只是挂起并需要时间来打开活动。我在寻找时遇到了这个解决方案:)
    • 使用android:fontFamily从布局设置字体会降低性能吗?注意:我使用的是可下载的 Google 字体..
    猜你喜欢
    • 2012-12-10
    • 1970-01-01
    • 1970-01-01
    • 2013-02-23
    • 1970-01-01
    • 1970-01-01
    • 2012-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多