一、前言
前几天发现09年Google IO大会关于移动应用方面的主题有一些不错的PPT,对移动应用开发很有帮助。自己看了一些,边看边和大家分享。既然是PPT就很简化了,我会根据实际情况写一些Demo供大家参考。
Android在UI优化方面可以从以下五个方面入手:
二、Adapter优化
什么是Adapter,可以先看看我的上一篇文章,Android开发——说说Adapter那点事 Adapter与View的连接主要依靠getView这个方法返回我们需要的自定义view。ListView是Android app中一个最最最常用的控件了,所以如何让ListView流畅运行,获取良好的用户体验是非常重要的。对ListView优化就是对Adapter中的getView方法进行优化。09年的Google IO大会给出的优化建议如下:
Adapter优化示例代码:
1: @Override
2: public View getView(int position, View convertView, ViewGroup parent) {
3: Log.d("MyAdapter", "Position:" + position + "---"
4: + String.valueOf(System.currentTimeMillis()));
5: ViewHolder holder;
6: if (convertView == null) {
7: final LayoutInflater inflater = (LayoutInflater) mContext
8: .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
9: convertView = inflater.inflate(R.layout.list_item_icon_text, null);
10: holder = new ViewHolder();
11: holder.icon = (ImageView) convertView.findViewById(R.id.icon);
12: holder.text = (TextView) convertView.findViewById(R.id.text);
13: convertView.setTag(holder);
14: } else {
15: holder = (ViewHolder) convertView.getTag();
16: }
17: holder.icon.setImageResource(R.drawable.icon);
18: holder.text.setText(mData[position]);
19: return convertView;
20: }
21:
22: static class ViewHolder {
23: ImageView icon;
24:
25: TextView text;
26: }
1: @Override
2: public View getView(int position, View convertView, ViewGroup parent) {
3: Log.d("MyAdapter", "Position:" + position + "---"
4: + String.valueOf(System.currentTimeMillis()));
5: final LayoutInflater inflater = (LayoutInflater) mContext
6: .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
7: View v = inflater.inflate(R.layout.list_item_icon_text, null);
8: ((ImageView) v.findViewById(R.id.icon)).setImageResource(R.drawable.icon);
9: ((TextView) v.findViewById(R.id.text)).setText(mData[position]);
10: return v;
11: }
两种情况在操作过程中体验明显不同,在优化的情况下流畅很多很多!
1、优化建议测试结果:
12-05 10:44:46.039: DEBUG/MyAdapter(13929): Position:0---1291517086043
12-05 10:44:46.069: DEBUG/MyAdapter(13929): Position:1---1291517086072
12-05 10:44:46.079: DEBUG/MyAdapter(13929): Position:2---1291517086085
……
12-05 10:45:04.109: DEBUG/MyAdapter(13929): Position:997---1291517104112
12-05 10:45:04.129: DEBUG/MyAdapter(13929): Position:998---1291517104135
12-05 10:45:04.149: DEBUG/MyAdapter(13929): Position:999---1291517104154
耗时:17967
2、没优化的测试结果
12-05 10:51:42.569: DEBUG/MyAdapter(14131): Position:0---1291517502573
12-05 10:51:42.589: DEBUG/MyAdapter(14131): Position:1---1291517502590
12-05 10:51:42.609: DEBUG/MyAdapter(14131): Position:2---1291517502617
……
12-05 10:52:07.079: DEBUG/MyAdapter(14131): Position:998---1291517527082
12-05 10:52:07.099: DEBUG/MyAdapter(14131): Position:999---1291517527108
耗时:24535
在1000条记录的情况下就有如此差距,一旦数据nW+,ListView的Item布局更加复杂的时候,优化的作用就更加突出了!
OK,欢迎大家交流学习,Gtalk:ghanguo@gmail.com
转载请注明出处!
示例代码: GoogleIO示例代码
---------EOF------------------