在重新设计应用程序后,我们在新视图创建时获得了更大的加载时间(旧版本几乎没有图像并且速度更快)。我们使用ViewFlipper浏览视图。创建的前两个视图是具有ListView的两个布局,其元素具有背景图像和其他图形。巧合的是,我们发现,每次创建一个新视图并将其放在视图堆栈上时,应用程序都会计算每个现有视图的尺寸(其中几次调用ListView适配器的getView()方法)。每个调用的跟踪日志如下所示:
LinearLayout.measureChildBeforeLayout(View,int,int,int,int,int)line:1369 LinearLayout.measureVertical(int,int)line:660 LinearLayout.onMeasure(int,int)line:553 LinearLayout(View).measure (int,int)行:12937 TabHost(ViewGroup).measureChildWithMargins(View,int,int,int,int)行:5045 TabHost(FrameLayout).onMeasure(int,int)行:293 TabHost(View).measure(int ,int)行:12937 RelativeLayout.measureChildHorizontal(View,RelativeLayout $ LayoutParams,int,int)行:594 RelativeLayout.onMeasure(int,int)行:376 RelativeLayout(View).measure(int,int)行:12937 FrameLayout( ViewGroup).measureChildWithMargins(View,int,int,int,int)行:5045 FrameLayout.onMeasure(int,int)行:293 FrameLayout(View).measure(int,int)行:12937 LinearLayout(ViewGroup).measureChildWithMargins(视图,int,int,int,int)行:5045 LinearLayout.measureChildBeforeLayout(View,int,int,int,int,int)行:1369 LinearLayout.measureVertical(int,int)行:660 LinearLayout.o nMeasure(int,int)行:553 LinearLayout(View).measure(int,int)行:12937 PhoneWindow $ DecorView(ViewGroup).measureChildWithMargins(View,int,int,int,int)行:5045 PhoneWindow $ DecorView(FrameLayout ).onMeasure(int,int)行:293 PhoneWindow $ DecorView.onMeasure(int,int)行:2180 PhoneWindow $ DecorView(View).measure(int,int)行:12937
依旧...... 我认为这就是为什么应用程序变得如此缓慢的原因。 我该怎么做才能阻止应用程序执行这些措施或使应用程序更快?
ListAdapter getView():
public View getView(int pos, View convertView, ViewGroup parent) {
View row;
Entry entry = null;
if (objects.get(pos) instanceof Entry) {
entry = (Entry) objects.get(pos) ;
Boolean isGroupedEntry;
if (entry.getGroup() != null && entry.isGroupedEntry()) {
isGroupedEntry = true;
} else {
isGroupedEntry = false;
}
if (convertView != null) {
row = convertView;
} else {
row = new EntryBoxFrameLayout(getContext());
}
((EntryBoxFrameLayout) row).configureFor(entry);
} else {
row = new View(ContentManager.getInstance().getContext());
}
row.setSelected(false);
row.setTag(pos);
return row;
}
在configureFor()中我设置了一些文本然后:
if (entry.getUserIsSignedUp() > 0) {
this.setBackgroundColor("4A4A4A");
this.findViewById(R.id.star).setVisibility(View.VISIBLE);
this.findViewById(R.id.entry_face).setBackgroundResource(R.drawable.entry_face_loggedin);
fulldate.setTextColor(Color.BLACK);
} else {
this.setBackgroundColor(entry.getColorHex());
this.findViewById(R.id.star).setVisibility(View.INVISIBLE);
this.findViewById(R.id.entry_face).setBackgroundResource(R.drawable.entry_face);
fulldate.setTextColor(Color.WHITE);
}
if (entry.getGroup() != null) {
this.findViewById(R.id.imgGroupArrow).setVisibility(View.VISIBLE);
if (entry.isGroupExpanded()) {
((ImageView)this.findViewById(R.id.imgGroupArrow)).setImageResource(R.drawable.entry_up_arrow);
} else {
((ImageView)this.findViewById(R.id.imgGroupArrow)).setImageResource(R.drawable.entry_down_arrow);
}
this.setGroupContainer(true);
} else {
this.findViewById(R.id.imgGroupArrow).setVisibility(View.INVISIBLE);
this.setGroupContainer(false);
}
String filename = entry.getCacheFilename();
Drawable entryimage = null;
if (filename != null) {
try {
FileInputStream input = ContentManager.getInstance().getContext().openFileInput(filename);
entryimage = new BitmapDrawable(ContentManager.getInstance().getContext().getResources(), input);
input.close();
} catch (Exception e) {
Log.e(e.getLocalizedMessage());
}
}
if (entryimage != null) {
this.getImageView().setImageDrawable(entryimage);
} else {
this.getImageView().setImageResource(R.drawable.entry_placeholder);
}
答案 0 :(得分:1)
getView()方法会降低您的应用程序速度。 listView回收视图对象以提高性能。
有很多学习资源可以解决这个问题。
来自Google I / O大会和listView的创建者:Romain Guy
关于图像的优化
有关更多代码示例: