【问题标题】:Slow transition of Activities: multiple "initializing inflate state" in LogCat活动的缓慢过渡:LogCat 中的多个“初始化膨胀状态”
【发布时间】:2013-03-18 03:20:59
【问题描述】:

为了在我的ListActivity 中提供自定义字体,我根据这个例子here 写了一个类CustomAdapter 扩展BaseAdapter

但是,如那里所述,我编写了 getView() 方法,如下所示:

public View getView(int position, View convertView, ViewGroup parent){
    String gameName = gameNames[position]; // gameName ist the String[] of the Custom Adapter

    TextView tv = new TextView(context);
    tv.setText(gameName);
    tv.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/gulim.ttf"));

    return tv;
}

这按预期工作。唯一令人不安的是,列表显示大约需要三到四秒钟(在这种情况下,这是一个很长的时间)。但是,在ListActivity 中,我将onItemClickListeners 设置为:

private void setOnItemClickListener(){
    getListView().setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int pos, long id){
            onClickEntryButton(((TextView) view).getText().toString());
        }
    });
}

private void onClickEntryButton(String gameName){
    Intent intent = new Intent(this, GameActivity.class);
    intent.putExtra("gameName", gameName);
    startActivity(intent);
    finish();
}

现在,当单击 ListItem 时,GameActivity 打开需要更多时间。这个Activity 只是几个TextViews,其中填充了从SQLite 数据库中获取的信息。同样在这里,我为每个TextView 设置了一个自定义字体。即使屏幕变黑 2-3 秒(出现应用程序崩溃),也会发生这种情况,然后出现新的 Activity。从应用程序的其他位置访问 Activity 不会发生这种情况。

在这两种情况下 - 访问ListActivity 和从ListActivity 访问GameActivity - 几个

“szipinf - 初始化膨胀状态”

消息出现在 LogCat 中。它们在这种情况下意味着什么?将onItemClickListeners 设置为我的CustomAdaptergetView() 方法会更好吗?有些东西似乎真的抑制了转换,但我不知道是什么,因为没有什么大的需要计算或处理(事实上,在 SQLite 数据库中恰好有两个条目,每 5 个字段)?

编辑 如果需要或需要,我当然可以提供更多代码。

【问题讨论】:

    标签: android android-listview custom-component android-logcat


    【解决方案1】:

    我遇到了完全相同的问题,你的问题给了我答案!

    我仍然不知道确切的根本原因,但这是因为在我的情况下多次从资产中读取自定义字体。 如果我的屏幕上有 10 个小部件,并且每个小部件都使用自定义字体,Android 每次都会从资产中加载它。这不仅导致我的活动转换变慢,而且在多次播放后也导致崩溃。

    所以我为我的字体创建了一个缓存,以避免每次都从资产中获取字体。

    我在我的实用程序类中添加了这段代码:

    private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();
    
    public static final String ASSET_PATH="assetPath";
    
    public static Typeface getFont(Context c, String assetPath) {
        synchronized (cache) {
            if (!cache.containsKey(assetPath)) {
                try {
                    Typeface t =(Typeface.createFromAsset(c.getAssets(),
                            "fonts/arial.ttf"));
                    cache.put(assetPath, t);
                } catch (Exception e) {
                    return null;
                }
            }
            return cache.get(assetPath);
        }
    }
    

    我创建了我的自定义类来 setTypeface

    public class MyButton extends Button {
    
    public MyButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    
    public MyButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    public MyButton(Context context) {
        super(context);
    }
    
    @Override
    public void setTypeface(Typeface tf) {
    
        super.setTypeface(Util.getFont(getContext(), Util.ASSET_PATH));
    }
    
    }
    

    变量assetPath可用于在运行时提供不同的字体

    编辑Here 是我作为库创建的自定义字体管理器,以使其更通用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-25
      • 1970-01-01
      • 1970-01-01
      • 2020-02-16
      • 1970-01-01
      • 1970-01-01
      • 2014-11-18
      相关资源
      最近更新 更多