Android - 如何提高复杂动态UI的性能

时间:2013-03-05 18:59:31

标签: android performance dynamic user-interface

我有一个相对复杂的嵌套线性布局和文本视图设置,这些设置都是在运行时根据用户偏好创建的。

问题在于启动应用程序需要大约五秒钟(智能手机时间是永恒的)。

enter image description here

Here is a diagram of the set up.我在滚动视图中有大约30个红色线性布局,每个布局包含2到30个紫色文本视图,包含在1到6个橙色线性布局中。

我有什么办法可以加快加载这个UI或者保存它,以便当用户退出并重新打开应用程序时,它不必重新生成所有内容吗?

谢谢。

我的代码如下,它使用this post.

中的populateLinks函数
public void displayUnits(){
    LinearLayout ll = new LinearLayout(MainActivity.this);
    ll = (LinearLayout) findViewById(R.id.fromunitslayout);

    if (ll.getChildCount() > 3) {
        ll.removeViewsInLayout(2,ll.getChildCount()-2);
    }

    fromunitlls = new ArrayList<LinearLayout>();
    fromunitlls.clear();

    int colorcount = 0;

    for (int utcount = 0; utcount < unittypes.size(); utcount++) {
        UnitType currenttype = null;
        for (int typecount = 0; typecount < unittypes.size(); typecount++) {
            if (unittypes.get(typecount).getOrder() == utcount) {
                currenttype = unittypes.get(typecount);
                Log.v("unittype disp", String.valueOf(unittypes.indexOf(currenttype)));             
            }
        }
        if (currenttype.getHidden() && !showallunits) {
            continue;
        }
        for (int unitcount = 0; unitcount < ft.size(); unitcount++) {
            if (ft.get(unitcount).getType().getID() == currenttype.getID()) {

                // Create and set up Red LLs
                LinearLayout llhorz = new LinearLayout(MainActivity.this);
                LinearLayout llvert = new LinearLayout(MainActivity.this);
                LinearLayout lllist = new LinearLayout(MainActivity.this);
                llhorz.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
                llhorz.setOrientation(LinearLayout.HORIZONTAL);
                llvert.setLayoutParams(new LayoutParams(140,LayoutParams.MATCH_PARENT));
                llvert.setOrientation(LinearLayout.VERTICAL);
                lllist.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
                lllist.setOrientation(LinearLayout.VERTICAL);

                if (colorcount % 2 != 0) {
                    llhorz.setBackgroundResource(R.color.DarkGreyT);
                }
                colorcount++;

                // Add Blue Text View to Vertical LL            
                AutoResizeTextViewSmall txtTypeName = new AutoResizeTextViewSmall(MainActivity.this);
                txtTypeName.setTag("unittype" + unittypes.indexOf(currenttype));
                txtTypeName.setPadding(5,0,5,0);
                txtTypeName.setText(currenttype.getName().toUpperCase().replace(" ","\n"));
                txtTypeName.setLayoutParams(new LayoutParams(140,LayoutParams.WRAP_CONTENT, Gravity.CENTER));
                txtTypeName.setTextSize(12);
                txtTypeName.setTypeface(Typeface.DEFAULT_BOLD);
                txtTypeName.setGravity(Gravity.CENTER);

                txtTypeName.setOnLongClickListener(new OnLongClickListener(){
                    public boolean onLongClick(View view){
                        String typenum = view.getTag().toString();
                        typenum = typenum.substring(8);
                        Log.v("typenum",typenum);
                        Intent intent = new Intent(MainActivity.this, CatSettingsActivity.class);
                        intent.putExtra("TYPENUMBER", Integer.parseInt(typenum));
                        startActivity(intent);
                        return false; 
                    }
                });
                llvert.addView(txtTypeName);
                fromunitlls.add(lllist);
                fromunitlls.get(fromunitlls.indexOf(lllist)).setId(unittypes.indexOf(currenttype));

                llhorz.addView(llvert);
                //llhorz.addView(txtTypeName);
                llhorz.addView(fromunitlls.get(fromunitlls.indexOf(lllist)));

                ll.addView(llhorz);

                txtTypeName.reSize();
                break;
            }
        }
    }

    // Call other function to populate red LLs with orange LLs and purple TVs
    for (int utcount = 0; utcount < unittypes.size(); utcount++) {
        if (unittypes.get(utcount).getHidden()) {
            continue;
        }
        for (int unitcount = 0; unitcount < ft.size(); unitcount++) {
            if (ft.get(unitcount).getType().getID() == unittypes.get(utcount).getID()) {
                populateLinks(unittypes.get(utcount), ft);
                break;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您可以尝试使用RelativeLayout而不是嵌套的线性布局来减少布局数量。