2013-01-21 116 views
2

我的布局花费了太多的时间来加载大约2-3秒在实际device.My布局有很多观点从40-60意见。我正在检查各种条件以及在OnCreate().So任何人都可以建议我如何加载我的活动更快。而大部分的意见都有背景图片。实际上我的整个布局是充满了ImageView。 下面的onCreate()布局花费太多的时间来加载android

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

在此处发布您的代码。 – asloob

+0

你需要看看时间在哪里。我只是放了一些日志打印,因为他们得到时间戳。 – Ifor

+0

@我为什么把日志打印出来? – sankettt

回答

0

尝试使用线程或者如果它不可能,请观察如果在您的方法中,OnCreate,是否存在对布局完成呈现之前占用资源的某些东西的调用。

+0

是的,我确实有调用共享偏好,我加载动画,并伴随它几个sounds.doing一些日期相关的计算和数据库编码 – sankettt

0

让onCreateComplete尽可能快。不要拿着它来加载你的意见。 启动AsyncTasks以加载findViewById中的视图。 然后在asyntasks onPostsExecute中,您可以为您的视图设置值,例如将图像设置为图像浏览等。

值得一试。

+0

我一定会试一试。你能告诉我哪些代码应该放在asynctask中吗? – sankettt

+0

可以帮助我在这方面的例子。我对这个东西相当陌生,尽管我阅读大量的东西是为了显示从互联网加载文件或与之相关的文件的进度。并随着加载的图像我有数据库和阅读的共享偏好以及。任何有关这方面的帮助将非常感激,并会对我很好 – sankettt

相关问题