布局花了太多时间来加载android

时间:2013-01-21 11:03:13

标签: android android-layout

我的布局花了太多时间在实际设备上加载大约2-3秒。我的布局有很多视图,范围从40-60个视图。我正在检查OnCreate()中的各种条件。所以有人可以建议我如何更快地加载我的活动。并且大多数视图都有背景图像。实际上我的整个布局都是ImageView。 以下是onCreate()

的代码段
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fueladd_layout);

    face2=Typeface.createFromAsset(getAssets(),"digital.ttf");
    typewriter=Typeface.createFromAsset(getAssets(),"veteran_typewriter.ttf");

    Display display=getWindowManager().getDefaultDisplay();
    width=display.getWidth();
    height=display.getHeight();

    dbc=new Databaseconnection(this);
    sdb=dbc.getWritableDatabase();
    sdb=dbc.getReadableDatabase();

    c = Calendar.getInstance();
    fyear = c.get(Calendar.YEAR);
    month = c.get(Calendar.MONTH);
    day = c.get(Calendar.DAY_OF_MONTH);
    date();


    mp1=MediaPlayer.create(getBaseContext(), R.raw.button_click);
    newcarmenu=(RelativeLayout) findViewById(R.id.newcarmenu);
    topmenu=(RelativeLayout) findViewById(R.id.topmenu);
    newnotelayout=(RelativeLayout) findViewById(R.id.newnotelayout);
    layout_mileage=(RelativeLayout) findViewById(R.id.layout_mileage);

    img_tabdashboard=(ImageView) findViewById(R.id.img_tabdashboard);
    img_tabdashboard.setOnClickListener(this);

    img_tabexpenses=(ImageView) findViewById(R.id.img_tabexpenses);
    img_tabexpenses.setImageDrawable(img_tabexpenses.getResources().getDrawable(R.drawable.tabexpensespressed));
    img_tabexpenses.setOnClickListener(this);

    img_tabfinancial=(ImageView) findViewById(R.id.img_tabfinancial);
    img_tabfinancial.setOnClickListener(this);

    img_tabtrip=(ImageView) findViewById(R.id.img_tabtrip);
    img_tabtrip.setOnClickListener(this);

    img_menu=(ImageView) findViewById(R.id.img_menu);
    img_menu.getLayoutParams().width=(int) (width*0.109375);
    img_menu.setOnClickListener(this);

    img_preferences=(ImageView) findViewById(R.id.img_preferences);
    img_preferences.getLayoutParams().width=(int) (width*0.109375);
    img_preferences.setOnClickListener(this);

    img_mycar=(ImageView)findViewById(R.id.img_mycar);
    img_mycar.setOnClickListener(this);

    imgnotesdone=(ImageView)findViewById(R.id.imgnotesdone);
    imgnotesdone.setOnClickListener(this);

    img_aboutus=(ImageView)findViewById(R.id.img_aboutus);
    img_aboutus.setOnClickListener(this);

    img_editcar=(ImageView)findViewById(R.id.img_editcar);
    img_editcar.setOnClickListener(this);

    img_exit=(ImageView)findViewById(R.id.img_exit);
    img_exit.setOnClickListener(this);

    img_fuel=(ImageView) findViewById(R.id.img_fuel);
    img_fuel.setBackgroundResource(R.drawable.fuel2);
    img_fuel.setOnClickListener(this);

    img_toll=(ImageView) findViewById(R.id.img_toll);
    img_toll.setOnClickListener(this);

    img_parking=(ImageView) findViewById(R.id.img_parking);
    img_parking.setOnClickListener(this);

    img_puc=(ImageView) findViewById(R.id.img_puc);
    img_puc.setOnClickListener(this);

    img_oil=(ImageView) findViewById(R.id.img_oil);
    img_oil.setOnClickListener(this);

    img_service=(ImageView) findViewById(R.id.img_service);
    img_service.setOnClickListener(this);

    img_add=(ImageView) findViewById(R.id.img_save);
    img_add.setOnClickListener(this);

    img_view=(ImageView) findViewById(R.id.img_view);
    img_view.setOnClickListener(this);

    img_addnewcar=(ImageView) findViewById(R.id.img_addnewcar);
    img_addnewcar.setOnClickListener(this);

    img_addnewbike=(ImageView) findViewById(R.id.img_addnewbike);
    img_addnewbike.setOnClickListener(this);

    img_usedcar=(ImageView) findViewById(R.id.img_usedcar);
    img_usedcar.setOnClickListener(this);

    img_usedcar=(ImageView) findViewById(R.id.img_usedbike);
    img_usedcar.setOnClickListener(this);

    img_menuexit=(ImageView) findViewById(R.id.img_menuexit);
    img_menuexit.setOnClickListener(this);

    img_loc_picker = (ImageView) this.findViewById(R.id.img_loc_picker);
    img_loc_picker.setOnClickListener(this);

    txt_title=(TextView) findViewById(R.id.txttitle);
    txt_title.setTypeface(face2,Typeface.BOLD);
    txt_title.setText("FUEL");


    layout_list=(RelativeLayout) findViewById(R.id.layout_list);

    animationBottomIn = AnimationUtils.loadAnimation(layout_list.getContext(), R.anim.slide_in_up);
    animationBottomOut = AnimationUtils.loadAnimation(layout_list.getContext(), R.anim.slide_out_down);

    animationRightIn = AnimationUtils.loadAnimation(layout_list.getContext(), R.anim.slide_in_right);
    animationRightOut = AnimationUtils.loadAnimation(layout_list.getContext(), R.anim.slide_out_right);


    list_type= (ListView) findViewById(R.id.list_type);
    list_type.setOnItemClickListener(this);

    list_location= (ListView) findViewById(R.id.list_location);
    list_location.setOnItemClickListener(this);

    txt_type_loc=(TextView) findViewById(R.id.txt_type_loc);
    txt_type_loc.setTypeface(typewriter);

    img_arrow_down = (ImageView) this.findViewById(R.id.img_arrow_down);
    img_arrow_down.setOnClickListener(this);

    txtselectdate=(TextView) findViewById(R.id.txt_currentdate);
    txtselectdate.setTypeface(typewriter);

    txtnewnotes=(TextView) findViewById(R.id.txtnewnotes);
    txtnewnotes.setTypeface(typewriter);

    txtcdate=(TextView) findViewById(R.id.txtcdate);
    txtcdate.setTypeface(typewriter);
    txtcdate.setOnClickListener(this);

    txtcurrentkm=(TextView) findViewById(R.id.txt_currentkm);
    txtcurrentkm.setTypeface(typewriter);

    txtpreviouskm=(TextView) findViewById(R.id.txt_previouskm);
    txtpreviouskm.setTypeface(typewriter);

    txtfuel=(TextView) findViewById(R.id.txt_quantity);
    txtfuel.setTypeface(typewriter);

    txtrate=(TextView) findViewById(R.id.txt_rate);
    txtrate.setTypeface(typewriter);

    txtamount=(TextView) findViewById(R.id.txt_amount);
    txtamount.setTypeface(typewriter);

    txtmileage=(TextView) findViewById(R.id.txt_mileage);
    txtmileage.setTypeface(typewriter);

    txtlocation=(TextView) findViewById(R.id.txt_location);
    txtlocation.setTypeface(typewriter);

    txtbrand=(TextView) findViewById(R.id.txt_brand);
    txtbrand.setTypeface(typewriter);

    txtnote=(TextView) findViewById(R.id.txt_note);
    txtnote.setTypeface(typewriter);

    editbrand=(EditText) findViewById(R.id.edit_brand);
    editbrand.setTypeface(typewriter);

    editlocation=(EditText) findViewById(R.id.edit_location);
    editlocation.setTypeface(typewriter);

    editnewnotes=(EditText) findViewById(R.id.editnewnotes);
    editnewnotes.setTypeface(typewriter);

    img_date_picker = (ImageView) this.findViewById(R.id.img_date_picker);
    img_date_picker.setOnClickListener(this);

    img_loc_picker = (ImageView) this.findViewById(R.id.img_loc_picker);
    img_loc_picker.setOnClickListener(this);

    img_brand_picker = (ImageView) this.findViewById(R.id.img_brand_picker);
    img_brand_picker.setOnClickListener(this);

    imgnotepicker = (ImageView) this.findViewById(R.id.imgnotepicker );
    imgnotepicker .setOnClickListener(this);

    //this will set the text of edit view
    //the 1st record value is retrived and displayed
    //if no record is present than editview is empty

    loc_select_Query = "select  item From FuelLocation";
    Cursor cursor_loc = sdb.rawQuery(loc_select_Query, null);
    int count=cursor_loc.getCount();
    cursor_loc.moveToFirst();
    if(count>1)
    {
        editlocation.setText(cursor_loc.getString(0));
    }

    //this will set the text of edit view of brand
    //the 1st record value is retrived and displayed
    //if no record is present than editview is empty

    brand_select_Query = "select  brand From FuelBrand";
    Log.i("select_Query", brand_select_Query);
    Cursor cursor_brand = sdb.rawQuery(brand_select_Query, null);
    int count1=cursor_brand.getCount();
    cursor_brand.moveToFirst();
    if(count1>1)
    {
        editbrand.setText(cursor_brand.getString(0));
    }

    editcurrentkm=(EditText) findViewById(R.id.edit_currentkm);
    editcurrentkm.setTypeface(typewriter);

    editpreviouskm=(EditText) findViewById(R.id.edit_previouskm);
    editpreviouskm.setTypeface(typewriter);
    editpreviouskm.setOnFocusChangeListener(new OnFocusChangeListener()
    {
        public void onFocusChange(View v, boolean hasFocus) 
        {
            if(!hasFocus)
            {
                String select="Select Fuel_CurrentKm from Fuel ";
                Cursor selectcur = sdb.rawQuery(select, null);
                selectcur.moveToLast();
                Log.i("Record Count",Integer.toString(selectcur.getCount()));

                if(selectcur.getCount()>0)
                {   
                    Log.i("currentkm",editcurrentkm.getText().toString());
                    Log.i("currentkm",editcurrentkm.getText().toString());
                    if(Integer.valueOf(editpreviouskm.getText().toString())<Integer.valueOf(selectcur.getString(0).toString()))
                    {
                        msg="Enter previous odometer greater than"+selectcur.getString(0).toString();
                        showToastMessage(msg);
                    }
                }
            }
        }
    });
    editcurrentkm.setOnFocusChangeListener(new OnFocusChangeListener() 
    {
        public void onFocusChange(View v, boolean hasFocus) 
        {
            if(!hasFocus)
            {
                ckm=editcurrentkm.getText().toString();

                if(ckm.equalsIgnoreCase(""))
                {
                    msg="Enter Current Odometer reading";
                    showToastMessage(msg);
                }
                else
                {
                    if(Integer.valueOf(editcurrentkm.getText().toString())>Integer.valueOf(editpreviouskm.getText().toString()))
                    {
                        if(pcount>1)
                        {
                            currentkm=Integer.valueOf(ckm);
                            calmileage=(currentkm-previouskm)/pquantity;
                            editmileage.setText(Float.toString(calmileage));
                            Log.i("Mileage",Float.toString(calmileage));
                        }
                    }
                    else 
                    {
                        msg="Enter Current Odometer reading Greater than Previous Odometer reading";
                        showToastMessage(msg);
                    }

                }
            }
        }
    });
    editfuel=(EditText) findViewById(R.id.edit_quantity);
    editfuel.setTypeface(typewriter);

    editrate=(EditText) findViewById(R.id.edit_rate);
    editrate.setTypeface(typewriter);
    editrate.setOnFocusChangeListener(new OnFocusChangeListener() 
    {
        public void onFocusChange(View arg0, boolean arg1) 
        {
            if(!arg1)
            {
                if(!editfuel.getText().toString().equalsIgnoreCase(""))
                {
                    fquantity=editfuel.getText().toString();
                    quantity=Float.valueOf(fquantity);
                    if(!editrate.getText().toString().equalsIgnoreCase(""))
                    {
                        frate=editrate.getText().toString();
                        total=quantity*Float.valueOf(frate);
                        editamount.setText(Float.toString(total));

                    }                       
                }   
            }
        }
    });

    editamount=(EditText) findViewById(R.id.edit_amount);
    editamount.setTypeface(typewriter);
    editamount.setOnFocusChangeListener(new OnFocusChangeListener() 
    {
        public void onFocusChange(View arg0, boolean arg1) 
        {
            if(!arg1)
            {
                if(!editfuel.getText().toString().equalsIgnoreCase(""))
                {
                    fquantity=editfuel.getText().toString();
                    quantity=Float.valueOf(fquantity);
                    if(!editamount.getText().toString().equalsIgnoreCase(""))
                    {
                        famount=editamount.getText().toString();
                        rate=Float.valueOf(famount)/quantity;
                        editrate.setText(Float.toString(rate));
                    }                       
                }   
            }
        }
    });

    editmileage=(EditText) findViewById(R.id.edit_mileage);
    editmileage.setTypeface(typewriter);

    editnote=(EditText) findViewById(R.id.edit_note);
    editnote.setTypeface(typewriter);

    SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
    readsound=myPrefs.getBoolean(SOUND,sound);
    car_id= myPrefs.getInt(MY_CAR,count);
    Log.i("car id",Integer.toString(car_id));
    if(car_id==0)
    {
        car_id=1;
        Log.i("Car_ID",Long.toString(car_id));
    }

    Intent editintent=getIntent();
    String action=editintent.getAction();
    Log.i("Action",action);

    if(action.equalsIgnoreCase("com.curioussolutions.autoistdiary.fuel.EDIT"))
    {
        Log.i("inside","edit");
        record=(Integer) getIntent().getSerializableExtra("Id");
        Log.i("Record",Integer.toString(record));

        Cursor editcursor = sdb.rawQuery("select Fuel_Date,Fuel_CurrentKm,Fuel_PreviousKm,Fuel_Quantity,Fuel_Rate,Fuel_Amount,Fuel_Milege,Fuel_Brand,Fuel_Location,Fuel_Note from Fuel where Fuel_Id="+record, null);
        count=editcursor.getCount();

        editcursor.moveToFirst();

        udate=editcursor.getString(0).toString();
        Log.i("date",udate);
        txtcdate.setText(udate);
        Log.i("date",udate);

        currentkm=Integer.valueOf(editcursor.getString(1).toString());
        editcurrentkm.setText(Integer.toString(currentkm));
        Log.i("currentkm",Integer.toString(currentkm));

        pkm=Integer.valueOf(editcursor.getString(2).toString());
        Log.i("previouskm",Integer.toString(pkm));
        editpreviouskm.setText(Integer.toString(pkm));
        Log.i("previouskm",Integer.toString(pkm));

        quantity=Float.valueOf(editcursor.getString(3).toString());
        editfuel.setText(Float.toString(quantity));
        Log.i("quantity",Float.toString(quantity));

        rate=Float.valueOf(editcursor.getString(4).toString());
        editrate.setText(Float.toString(rate));
        Log.i("rate",Float.toString(rate));

        total=Float.valueOf(editcursor.getString(5).toString());
        editamount.setText(Float.toString(total));
        Log.i("cost",Float.toString(total));
        if(record>1)
        {
            calmileage=Float.valueOf(editcursor.getString(6).toString());
            editmileage.setText(Float.toString(calmileage));
            Log.i("mileage",Float.toString(calmileage));
        }
        if(record==1)
        {
            layout_mileage.setVisibility(RelativeLayout.GONE);
        }
        location=editcursor.getString(7).toString();
        editlocation.setText(location);
        Log.i("location",location);

        fbrand=editcursor.getString(8).toString();
        editbrand.setText(fbrand);
        Log.i("date",fbrand);

        notes=editcursor.getString(9).toString();
        editnote.setText(notes);
        Log.i("notes",notes);
    }
    if(action.equalsIgnoreCase("com.curioussolutions.autoistdiary.fuel.ADD"))
    {
        Log.i("inside","add");
        String select1="select Fuel_CurrentKm,Fuel_Quantity,Fuel_Rate from Fuel";
        Cursor cur = sdb.rawQuery(select1, null);
        pcount=cur.getCount()+1;
        cur.moveToLast();

        if(pcount>1)
        {   
            previouskm=Integer.valueOf(cur.getString(0).toString());
            pquantity=Float.valueOf(cur.getString(1).toString());
            rate=Float.valueOf(cur.getString(2).toString());
            editpreviouskm.setText(Integer.toString(previouskm));
            editrate.setText(Float.toString(rate));
        }
        else
        {
            layout_mileage.setVisibility(RelativeLayout.GONE);
            editpreviouskm.setText("0");
        }
        date();
        txtcdate.setText(date);

    }
    else
    {
        layout_mileage.setVisibility(RelativeLayout.GONE);
        editpreviouskm.setText("0");
        date();
        txtcdate.setText(date);
    }
}

3 个答案:

答案 0 :(得分:1)

以下是一个非常好的解释,您如何优化您的用户界面,以便加快加载速度:Android Performance in Practice by Romain Guy。 我建议你看看。

答案 1 :(得分:0)

尝试使用线程,或者如果在您的方法OnCreate中无法监视是否在完成呈现布局之前调用消耗资源的内容。

答案 2 :(得分:0)

让onCreateComplete尽快完成。不要用它来加载你的观点。 启动AsyncTasks以在findViewById中加载视图。 然后在asyntasks onPostsExecute中,您可以为您的视图设置值,例如将图像设置为您的图像视图等。

值得一试。