所以我定义了一个基于LinearLayout的自定义视图:
public class AlphabetButton extends LinearLayout{
private Button alphabetButton;
private ImageView usersMark;
...
public AlphabetButton(Context context) {
super(context);
load(context);
}
...
private void load(Context context){
if(isInEditMode())
return;
LayoutInflater.from(context).inflate(R.layout.alphabet_button, this, true);
alphabetButton = (Button)findViewById(R.id.buttonAlphabetItem);
usersMark = (ImageView)findViewById(R.id.correctWrongSelectedMark);
alphabetButton.setTag(usersMark);
final Typeface chalkFont = Typeface.createFromAsset(context.getAssets(), "fonts/myfont.ttf");
alphabetButton.setTypeface(chalkFont);
}
由于应用程序创建了大约 30 个此视图,因此它有明显的滞后(在 CPU 较弱的智能手机上大约需要 2-3 秒)。我还注意到一个日志输出(不是我的),例如:
05-01 16:47:22.224: D/szipinf(10569): Initializing inflate state
05-01 16:47:22.234: D/szipinf(10569): Initializing inflate state
05-01 16:47:22.254: D/szipinf(10569): Initializing inflate state
05-01 16:47:22.264: D/szipinf(10569): Initializing inflate state
05-01 16:47:22.334: D/szipinf(10569): Initializing inflate state
大约有 30 行相同的行。所以我认为load(Context context)
方法,尤其是膨胀过程是滞后的资源。但是如何避免呢?如何优化此视图以更快地实例化它?