当前位置: 代码迷 >> Android >> android平板下的GridView视图缓存优化
  详细解决方案

android平板下的GridView视图缓存优化

热度:45   发布时间:2016-05-01 20:17:19.0
android平板上的GridView视图缓存优化

?

本文来自http://blog.csdn.net/hellogv/?,引用必须注明出处!

????? 最近在做android平板上的开发,其中涉及到高分辨率之下使用GridView的性能问题。在Android手机软件开发中,如果在ListView或者GridView上使用大数量Item,很多人都会想到ViewHolder......没错,ViewHolder非常适合用在ListView或者每行小于4个Item的GridView。但是如果是高分辨率的设备(android平板甚至android电视),每行包含4个以上Item的话,即使用了ViewHolder也依然卡。

????? 如下图,每行9个Item,而且每个Item的图片都是从网络动态下载的,这时就比较考验GridView视图的优化了。

????? 本文提出的优化方法是:在getView()构建一个View列表(List<View>),把最近构建的View存起来,回退时直接从View列表中读取,而不是动态构建。使用这种方法有2个好处:

1.快速读取过去的Item;

2.直接保存View而不是Bitmap,避免了ImageView.setImageBitmaps()带来的延时。

当然坏处就是浪费内存,所以要设定一个上限,超过了就删掉最老的Item。
先来看看这种方法与ViewHolder的性能对比:


100个Item往下滚到的三组数据对比,如上图:
“CacheAdapter 缓存50个Item”跟ViewHolderAdapter的速度很接近,由于CacheAdapter有缓存,所以会有1~2次快速读取Item(10~20个)的情况,而ViewHolder的每次读取Item速度比较平均。
“CacheAdapter 缓存75个Item”只在第一次往下滚动时消耗较长时间,第二次用了缓存的Item,所以速度快了很多。

?

?

100个Item往上滚到的三组数据对比,如上图:

“CacheAdapter 缓存50个Item”比ViewHolderAdapter的速度略快,“CacheAdapter 缓存75个Item”依然是最快的。
总结:“CacheAdapter 缓存50个Item”速度与HolderView略快,读取最近的Item速度最快,缓存的Item越多速度越快。“CacheAdapter 缓存75个Item”占用内存最少,这是由于一部分图片下载失败,保存的Item的图片为空,实际上是缓存越多Item占用的内存越多。

PS:这里用到异步读取网络图片,成功下载的就占用较多内存,下载失败就占用较少内存,所以内存占用情况并不是一个时刻的绝对值,占用内存只用于参考.....

本文程序源码可以到http://www.rayfile.com/zh-cn/files/5ebf5666-958a-11e0-99ec-0015c55db73d/这里下载。

CacheAdapter.java是实现缓存Item的自定义Adapter,源码如下:

?

[java]?view plaincopyprint?
  1. /**?
  2. ?*?使用列表缓存过去的Item?
  3. [email protected]?
  4. ?*??
  5. ?*/??
  6. public?class?CacheAdapter?extends?BaseAdapter?{??
  7. ??
  8. ????public?class?Item?{??
  9. ????????public?String?itemImageURL;??
  10. ????????public?String?itemTitle;??
  11. ????????public?Item(String?itemImageURL,?String?itemTitle)?{??
  12. ????????????this.itemImageURL?=?itemImageURL;??
  13. ????????????this.itemTitle?=?itemTitle;??
  14. ????????}??
  15. ????}??
  16. ??
  17. ????private?Context?mContext;??
  18. ????private?ArrayList<Item>?mItems?=?new?ArrayList<Item>();??
  19. ????LayoutInflater?inflater;??
  20. ????public?CacheAdapter(Context?c)?{??
  21. ????????mContext?=?c;??
  22. ????????inflater?=?(LayoutInflater)?mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);??
  23. ????}??
  24. ??
  25. ????public?void?addItem(String?itemImageURL,?String?itemTitle)?{??
  26. ????????mItems.add(new?Item(itemImageURL,?itemTitle));??
  27. ????}??
  28. ??
  29. ????public?int?getCount()?{??
  30. ????????return?mItems.size();??
  31. ????}??
  32. ??
  33. ????public?Item?getItem(int?position)?{??
  34. ????????return?mItems.get(position);??
  35. ????}??
  36. ??
  37. ????public?long?getItemId(int?position)?{??
  38. ????????return?position;??
  39. ????}??
  40. ??????
  41. ????List<Integer>?lstPosition=new?ArrayList<Integer>();??
  42. ????List<View>?lstView=new?ArrayList<View>();??
  43. ??????
  44. ????List<Integer>?lstTimes=?new?ArrayList<Integer>();??
  45. ????long?startTime=0;??
  46. ????public?View?getView(int?position,?View?convertView,?ViewGroup?parent)?{??
  47. ????????startTime=System.nanoTime();??
  48. ??????????
  49. ????????if?(lstPosition.contains(position)?==?false)?{??
  50. ????????????if(lstPosition.size()>75)//这里设置缓存的Item数量??
  51. ????????????{??
  52. ????????????????lstPosition.remove(0);//删除第一项??
  53. ????????????????lstView.remove(0);//删除第一项??
  54. ????????????}??
  55. ????????????convertView?=?inflater.inflate(R.layout.item,?null);??
  56. ????????????TextView?text?=?(TextView)?convertView.findViewById(R.id.itemText);??
  57. ????????????ImageView?icon?=?(ImageView)?convertView.findViewById(R.id.itemImage);??
  58. ????????????text.setText(mItems.get(position).itemTitle);??
  59. ????????????new?AsyncLoadImage().execute(new?Object[]?{?icon,mItems.get(position).itemImageURL?});??
  60. ??????????????
  61. ????????????lstPosition.add(position);//添加最新项??
  62. ????????????lstView.add(convertView);//添加最新项??
  63. ????????}?else??
  64. ????????{??
  65. ????????????convertView?=?lstView.get(lstPosition.indexOf(position));??
  66. ????????}??
  67. ??????????
  68. ????????int?endTime=(int)?(System.nanoTime()-startTime);??
  69. ????????lstTimes.add(endTime);??
  70. ????????if(lstTimes.size()==10)??
  71. ????????{??
  72. ????????????int?total=0;??
  73. ????????????for(int?i=0;i<lstTimes.size();i++)??
  74. ????????????????total=total+lstTimes.get(i);??
  75. ??????
  76. ????????????Log.e("10个所花的时间:"?+total/1000?+"?μs",??
  77. ????????????????????"所用内存:"+Runtime.getRuntime().totalMemory()/1024?+"?KB");??
  78. ????????????lstTimes.clear();??
  79. ????????}??
  80. ??????????
  81. ????????return?convertView;??
  82. ????}??
  83. ??
  84. ????/**?
  85. ?????*?异步读取网络图片?
  86. [email protected]?
  87. ?????*/??
  88. ????class?AsyncLoadImage?extends?AsyncTask<Object,?Object,?Void>?{??
  89. ????????@Override??
  90. ????????protected?Void?doInBackground(Object...?params)?{??
  91. ??
  92. ????????????try?{??
  93. ????????????????ImageView?imageView=(ImageView)?params[0];??
  94. ????????????????String?url=(String)?params[1];??
  95. ????????????????Bitmap?bitmap?=?getBitmapByUrl(url);??
  96. ????????????????publishProgress(new?Object[]?{imageView,?bitmap});??
  97. ????????????}?catch?(MalformedURLException?e)?{??
  98. ????????????????Log.e("error",e.getMessage());??
  99. ????????????????e.printStackTrace();??
  100. ????????????}?catch?(IOException?e)?{??
  101. ????????????????Log.e("error",e.getMessage());??
  102. ????????????????e.printStackTrace();??
  103. ????????????}??
  104. ????????????return?null;??
  105. ????????}??
  106. ??
  107. ????????protected?void?onProgressUpdate(Object...?progress)?{??
  108. ????????????ImageView?imageView?=?(ImageView)?progress[0];??
  109. ????????????imageView.setImageBitmap((Bitmap)?progress[1]);???????????
  110. ????????}??
  111. ????}??
  112. ??
  113. ????static?public?Bitmap?getBitmapByUrl(String?urlString)??
  114. ????????????throws?MalformedURLException,?IOException?{??
  115. ????????URL?url?=?new?URL(urlString);??
  116. ????????URLConnection?connection?=?url.openConnection();??
  117. ????????connection.setConnectTimeout(25000);??
  118. ????????connection.setReadTimeout(90000);??
  119. ????????Bitmap?bitmap?=?BitmapFactory.decodeStream(connection.getInputStream());??
  120. ????????return?bitmap;??
  121. ????}??
  122. }??

?

其中if(lstPosition.size()>75)是设置缓存的Item数量的关键地方,这里缓存75个Item。

ViewHolderAdapter.java是实现ViewHolder加载Item的自定义Adapter,源码如下:

?

[java]?view plaincopyprint?
  1. /**?
  2. ?*?使用ViewHolder加载Item?
  3. [email protected]?
  4. ?*??
  5. ?*/??
  6. public?class?ViewHolderAdapter?extends?BaseAdapter?{??
  7. ??
  8. ????public?class?Item?{??
  9. ????????public?String?itemImageURL;??
  10. ????????public?String?itemTitle;??
  11. ??
  12. ????????public?Item(String?itemImageURL,?String?itemTitle)?{??
  13. ????????????this.itemImageURL?=?itemImageURL;??
  14. ????????????this.itemTitle?=?itemTitle;??
  15. ????????}??
  16. ????}??
  17. ??
  18. ????private?Context?mContext;??
  19. ????private?ArrayList<Item>?mItems?=?new?ArrayList<Item>();??
  20. ????LayoutInflater?inflater;??
  21. ????public?ViewHolderAdapter(Context?c)?{??
  22. ????????mContext?=?c;??
  23. ????????inflater?=?(LayoutInflater)?mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);??
  24. ????}??
  25. ??
  26. ????public?void?addItem(String?itemImageURL,?String?itemTitle)?{??
  27. ????????mItems.add(new?Item(itemImageURL,?itemTitle));??
  28. ????}??
  29. ??????
  30. ????public?int?getCount()?{??
  31. ????????return?mItems.size();??
  32. ????}??
  33. ??
  34. ????public?Item?getItem(int?position)?{??
  35. ????????return?mItems.get(position);??
  36. ????}??
  37. ??
  38. ????public?long?getItemId(int?position)?{??
  39. ????????return?position;??
  40. ????}??
  41. ??????
  42. ????static?class?ViewHolder?{??
  43. ????????TextView?text;??
  44. ????????ImageView?icon;??
  45. ????}??
  46. ??????
  47. ????List<Integer>?lstTimes=?new?ArrayList<Integer>();??
  48. ????long?startTime=0;??
  49. ????public?View?getView(int?position,?View?convertView,?ViewGroup?parent)?{??
  50. ????????startTime=System.nanoTime();??
  51. ??????????
  52. ????????ViewHolder?holder;??
  53. ??
  54. ????????if?(convertView?==?null)?{??
  55. ????????????convertView?=?inflater.inflate(R.layout.item,?null);??
  56. ????????????holder?=?new?ViewHolder();??
  57. ????????????holder.text?=?(TextView)?convertView.findViewById(R.id.itemText);??
  58. ????????????holder.icon?=?(ImageView)?convertView.findViewById(R.id.itemImage);??
  59. ????????????convertView.setTag(holder);??
  60. ????????}?else?{??
  61. ????????????holder?=?(ViewHolder)?convertView.getTag();??
  62. ????????}??
  63. ????????holder.text.setText(mItems.get(position).itemTitle);??
  64. ????????new?AsyncLoadImage().execute(new?Object[]{holder.icon,mItems.get(position).itemImageURL?});??
  65. ??????????
  66. ????????int?endTime=(int)?(System.nanoTime()-startTime);??
  67. ????????lstTimes.add(endTime);??
  68. ????????if(lstTimes.size()==10)??
  69. ????????{??
  70. ????????????int?total=0;??
  71. ????????????for(int?i=0;i<lstTimes.size();i++)??
  72. ????????????????total=total+lstTimes.get(i);??
  73. ??????
  74. ????????????Log.e("10个所花的时间:"?+total/1000?+"?μs",??
  75. ????????????????????"所用内存:"+Runtime.getRuntime().totalMemory()/1024?+"?KB");??
  76. ????????????lstTimes.clear();??
  77. ????????}??
  78. ??????????
  79. ????????return?convertView;??
  80. ????}??
  81. ??
  82. ????/**?
  83. ?????*?异步读取网络图片?
  84. [email protected]?
  85. ?????*/??
  86. ????class?AsyncLoadImage?extends?AsyncTask<Object,?Object,?Void>?{??
  87. ????????@Override??
  88. ????????protected?Void?doInBackground(Object...?params)?{??
  89. ??
  90. ????????????try?{??
  91. ????????????????ImageView?imageView=(ImageView)?params[0];??
  92. ????????????????String?url=(String)?params[1];??
  93. ????????????????Bitmap?bitmap?=?CacheAdapter.getBitmapByUrl(url);??
  94. ????????????????publishProgress(new?Object[]?{imageView,?bitmap});??
  95. ????????????}?catch?(MalformedURLException?e)?{??
  96. ????????????????e.printStackTrace();??
  97. ????????????}?catch?(IOException?e)?{??
  98. ????????????????e.printStackTrace();??
  99. ????????????}??
  100. ????????????return?null;??
  101. ????????}??
  102. ??
  103. ????????protected?void?onProgressUpdate(Object...?progress)?{??
  104. ????????????ImageView?imageView?=?(ImageView)?progress[0];??
  105. ????????????imageView.setImageBitmap((Bitmap)?progress[1]);??
  106. ????????}??
  107. ????}??
  108. ??
  109. }??

?

testPerformance.java是主程序,通过注释符就可以分别测试CacheAdapter与ViewHolderAdapter的性能,源码如下:

?

[java]?view plaincopyprint?
  1. public?class?testPerformance?extends?Activity?{??
  2. ????/**?Called?when?the?activity?is?first?created.?*/??
  3. ????@Override??
  4. ????public?void?onCreate(Bundle?savedInstanceState)?{??
  5. ????????super.onCreate(savedInstanceState);??
  6. ????????setContentView(R.layout.main);??
  7. ????????this.setTitle("android平板上的GridView视图缓存优化-----hellogv");??
  8. ????????GridView?gridview?=?(GridView)?findViewById(R.id.gridview);??
  9. ????????CacheAdapter?adapter=new?CacheAdapter(this);??
  10. ????????//?ViewHolderAdapter?adapter=new?ViewHolderAdapter(this);??
  11. ?????????
  12. ????????gridview.setAdapter(adapter);??
  13. ????????String?urlImage="";//请自己选择网络上的静态图片??
  14. ??????????
  15. ????????for(int?i=0;i<100;i++)??
  16. ????????{??
  17. ????????????adapter.addItem(urlImage,?"第"+i+"项");??
  18. ????????}??
  19. ??????????
  20. ????}??
  21. } ?

?

  相关解决方案