Displaying Bitmaps Efficiently[高效地显示Bitmaps]
- 这节课会介绍一些通用的用来处理与加载Bitmap对象的方法,这些技术能够使得不会卡到程序的UI并且避免程序消耗过度内存.如果你不注意这些,Bitmaps会迅速的消耗你可用的内存而导致程序crash,出现下面的异常:
java.lang.OutofMemoryError: bitmap size exceeds VM budget
. - 有许多原因说明在你的Android程序中加载Bitmaps是非常棘手的,需要你特别注意:
- 移动设备的系统资源有限。Android设备对于单个程序至少需要16MB的内存。Android Compatibility Definition Document (CDD), Section 3.7. Virtual Machine Compatibility 给出了对于不同大小与密度的屏幕的最低内存需求. 程序应该在这个最低内存限制下最优化程序的效率。当然,大多数设备的都有更高的限制需求.
- Bitmap会消耗很多内存,特别是对于类似照片等更加丰富的图片. 例如,Galaxy Nexus的照相机能够拍摄2592x1936 pixels (5 MB)的图片. 如果bitmap的配置是使用
ARGB_8888
(the default from the Android 2.3 onward) ,那么加载这张照片到内存会大概需要19MB(2592*1936*4 bytes) 的内存, 这样的话会迅速消耗掉设备的整个内存. - Android app的UI通常会在一次操作中立即加载许多张bitmaps. 例如在
ListView
,GridView
与ViewPager
等组件中通常会需要一次加载许多张bitmaps,而且需要多加载一些内容为了用户可能的滑动操作。
下面是这一章的官方Sample代码:
- Android 2.1 (API Level 7) or higher
- Support Library
Lessons
这一章我们将学习下面4个内容,需要提前看的同学请点击下面文章的链接直接看官方原文,在逐一成文之后,我会添加本博客对应课程的链接,谢谢!
-
Loading Large Bitmaps Efficiently
【Android Advanced Training - 09】高效地显示Bitmap图片 [ Lesson 1 - 有效加载偏大的图片]
- This lesson walks you through decoding large bitmaps without exceeding the per application memory limit. Processing Bitmaps Off the UI Thread
-
Bitmap processing (resizing, downloading from a remote source, etc.) should never take place on the main UI thread. This lesson walks you through processing bitmaps in a background thread using
AsyncTask
and explains how to handle concurrency issues.
Caching Bitmaps
- This lesson walks you through using a memory and disk bitmap cache to improve the responsiveness and fluidity of your UI when loading multiple bitmaps. Displaying Bitmaps in Your UI
-
This lesson brings everything together, showing you how to load multiple bitmaps into components like
ViewPager
andGridView
using a background thread and bitmap cache.
【Android Advanced Training - 09】高效地显示Bitmap图片 [ Lesson 2 - 在UI线程之外处理Bitmaps ]
【Android Advanced Training - 09】高效地显示Bitmap图片 [ Lesson 3 - 两种缓存Bitmap的方式 ]
【Android Advanced Training - 09】高效地显示Bitmap图片 [ Lesson 4 - 使用ViewPager与GridView显示图片 ]
最近Google官方的训练课程更新比较多,显然,需要加大学习的力度来紧跟步伐了,不过里面有些课程可能用到的不是很多,我会有选择性的优先学习并翻译。
转载请注明出处:
http://blog.csdn.net/kesenhoo,谢谢!