【发布时间】:2015-10-02 04:28:13
【问题描述】:
我有12个按钮分为2组,每组有6个按钮,所有按钮都响应一个长onClick方法goToCategory()。
我可以将它重构为许多小的独立 onclick 方法。
点击/触摸发生后,我的应用需要花费太多时间来渲染图像 - 大约 2-3 秒。我启动了 ddms 以查看发生了什么,进行了跟踪,并且我的应用程序偶然发现了 goToCategory() - 至少我认为这是导致渲染长时间延迟的根本问题。我可能想重写 long onclick 方法。
从性能的角度来看,什么更好?
public void goToCategory(View v){
switch (v.getId()){
case R.id.scientists:
categories.setVisibility(View.GONE);
hero1.setBackgroundResource(R.drawable.galim1);
hero2.setBackgroundResource(R.drawable.galim2);
hero3.setBackgroundResource(R.drawable.galim3);
upper_category_index = "science";
break ;
case R.id.scientists2:
categories2.setVisibility(View.GONE);
hero4.setBackgroundResource(R.drawable.galim1);
hero5.setBackgroundResource(R.drawable.galim2);
hero6.setBackgroundResource(R.drawable.galim3);
lower_category_index = "science";
break ;
case R.id.politics:
categories.setVisibility(View.GONE);
hero1.setBackgroundResource(R.drawable.pol1);
hero2.setBackgroundResource(R.drawable.pol2);
hero3.setBackgroundResource(R.drawable.pol3);
upper_category_index = "politics";
break ;
case R.id.politics2:
categories2.setVisibility(View.GONE);
hero4.setBackgroundResource(R.drawable.pol1);
hero5.setBackgroundResource(R.drawable.pol2);
hero6.setBackgroundResource(R.drawable.pol3);
lower_category_index = "politics";
break ;
case R.id.akins:
categories.setVisibility(View.GONE);
hero1.setBackgroundResource(R.drawable.akin1);
hero2.setBackgroundResource(R.drawable.akin2);
hero3.setBackgroundResource(R.drawable.akin3);
upper_category_index = "akin";
break ;
case R.id.akins2:
categories2.setVisibility(View.GONE);
hero4.setBackgroundResource(R.drawable.akin1);
hero5.setBackgroundResource(R.drawable.akin2);
hero6.setBackgroundResource(R.drawable.akin3);
lower_category_index = "akin";
break ;
case R.id.folk_heroes:
categories.setVisibility(View.GONE);
hero1.setBackgroundResource(R.drawable.folk1);
hero2.setBackgroundResource(R.drawable.folk2);
hero3.setBackgroundResource(R.drawable.folk3);
upper_category_index = "folk";
break ;
case R.id.folk_heroes2:
categories2.setVisibility(View.GONE);
hero4.setBackgroundResource(R.drawable.folk1);
hero5.setBackgroundResource(R.drawable.folk2);
hero6.setBackgroundResource(R.drawable.folk3);
lower_category_index = "folk";
break ;
case R.id.hans:
categories.setVisibility(View.GONE);
hero1.setBackgroundResource(R.drawable.han1);
hero2.setBackgroundResource(R.drawable.han2);
hero3.setBackgroundResource(R.drawable.han3);
upper_category_index = "hans";
break ;
case R.id.hans2:
categories2.setVisibility(View.GONE);
hero4.setBackgroundResource(R.drawable.han1);
hero5.setBackgroundResource(R.drawable.han2);
hero6.setBackgroundResource(R.drawable.han3);
lower_category_index = "hans";
break ;
case R.id.batirs:
categories.setVisibility(View.GONE);
hero1.setBackgroundResource(R.drawable.kabanbai);
hero2.setBackgroundResource(R.drawable.bogenbai);
hero3.setBackgroundResource(R.drawable.karasai);
upper_category_index = "batirs";
break ;
case R.id.batirs2:
categories2.setVisibility(View.GONE);
hero4.setBackgroundResource(R.drawable.kabanbai);
hero5.setBackgroundResource(R.drawable.bogenbai);
hero6.setBackgroundResource(R.drawable.karasai);
lower_category_index = "batirs";
break ;
}
}
【问题讨论】:
-
无论哪种方式都不会有明显的性能差异,尤其是对于这种很少被调用的方法。
标签: android performance onclick dalvik ddms