活动缓慢转换:LogCat中多次"初始化膨胀状态"

Val*_* Ru 6 android custom-component android-listview android-logcat

为了在我的脑中提供自定义字体,我在这里ListActivity写了一个根据这个例子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;
}
Run Code Online (Sandbox Code Playgroud)

这按预期工作.唯一令人不安的是,在列表显示之前需要大约三到四秒钟(在这种情况下这是一个很长的时间).但是,在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();
}
Run Code Online (Sandbox Code Playgroud)

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

在这两种情况下-访问ListActivity和访问GameActivity来自ListActivity-一对夫妇的

"szipinf - 初始化膨胀状态"

消息显示在LogCat中.在这种情况下,它们意味着什么?将onItemClickListeners 设置getView()为我的方法会更好CustomAdapter吗?有些东西似乎真的会抑制过渡,但我无法弄清楚是什么,因为没有什么可以计算或处理的大事(事实上,在SQLite数据库中,每5个字段恰好有两个条目)?

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

Nay*_*pte 4

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

我仍然不知道确切的根本原因,但这是因为在我的情况下多次从资产中读取自定义字体。如果我的屏幕上有 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

我创建了自定义类来设置Typeface

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

}
Run Code Online (Sandbox Code Playgroud)

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

编辑是我创建的自定义 typefaceManager 作为库,以使其更加通用。