Bitmap

来源:互联网 发布:淘宝精品推荐券 编辑:程序博客网 时间:2024/06/05 22:46

译文:https://developer.android.com/training/displaying-bitmaps/index.html


Learn how to use common techniques to process and load Bitmap objects in a way that keeps your user interface (UI) components responsive and avoids exceeding your application memory limit. If you’re not careful, bitmaps can quickly consume your available memory budget leading to an application crash due to the dreaded exception:java.lang.OutofMemoryError: bitmap size exceeds VM budget.

学会使用一般技巧来处理和加载位图对象,这样可以使你的用户接口更具响应性并且可以避免应用程序内存的限制。如果你不管这些的话,位图可能会很快的消耗你可用的内存,并导致可怕的异常即内存不足异常。



There are a number of reasons why loading bitmaps in your Android application is tricky:

  1. Mobile devices typically have constrained system resources. Android devices can have as little as 16MB of memory available to a single application. The Android Compatibility Definition Document (CDD), Section 3.7. Virtual Machine Compatibility gives the required minimum application memory for various screen sizes and densities. Applications should be optimized to perform under this minimum memory limit. However, keep in mind many devices are configured with higher limits.

  2. Bitmaps take up a lot of memory, especially for rich images like photographs. For example, the camera on the Galaxy Nexus takes photos up to 2592x1936 pixels (5 megapixels). If the bitmap configuration used is ARGB_8888 (the default from the Android 2.3 onward) then loading this image into memory takes about 19MB of memory (2592*1936*4 bytes), immediately exhausting the per-app limit on some devices.

  3. Android app UI’s frequently require several bitmaps to be loaded at once. Components such as ListView, GridView and ViewPager commonly include multiple bitmaps on-screen at once with many more potentially off-screen ready to show at the flick of a finger.



有以下三点原因解释了为什么加载位图是一件麻烦的事情:

  1. 移动设备一般会限制系统资源。对于单个应用程序,安卓设备可能会分配16M左右的内存。 安卓兼容性定义文档,第3.7部分,虚拟机兼容性部分对不同不同尺寸与密度的安卓设备提供了需要的最小的应用程序内存限制。在这个最小内存条件限制下应用程序应该优化后再运行。然后,许多设备配置了更高的限制。

  2. 位图占用了大量内存,特别是对丰富的图像,如照片。例如 Galaxy Nexus 拍摄了2592x1936(500万像素)。如果位图配置使用的是 ARGB_8888(Android2.3以后)格式的话,那么我们加载这个图片需要占用19M内存,这会立即消耗一些设备中每个应用的内存限制。

  3. 安卓的UI接口需要频繁地立即加载位图。ListView,GridView,ViewPager这些组件一般包括多个位图,并且通过手指滑动可以在屏幕上显示更多的隐藏位图。


Lessons

  1. Loading Large Bitmaps Efficiently
    This lesson walks you through decoding large bitmaps without exceeding the per application memory limit.

  2. 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.

  3. 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.

  4. Managing Bitmap Memory
    This lesson explains how to manage bitmap memory to maximize your app’s performance.

  5. Displaying Bitmaps in Your UI
    This lesson brings everything together, showing you how to load multiple bitmaps into components like ViewPager and GridView using a background thread and bitmap cache.


课程

  • 更有效地加载大型位图
    本课程教你不超出每个应用程序内存限制的情况下编码较大位图

  • 在非UI线程中处理位图
    位图处理(大小调节,从远程下载)这些不应该发生在UI线程中。本课程带你通过使用AsyncTask将图片在背景线程中处理,并解释如何处理并发问题。

  • 缓存位图
    本课程教你在加载多张位图的情况下,如何通过使用内存缓存与存储器缓存来改善UI的反应性与流动性

  • 管理位图内存
    本课程解释如何通过管理位图内存来尽可能提高程序性能

  • 在UI中展示位图
    本课程将以上综合在一起,通过使用背景线程与位图缓存机制向你展示如何将多张图片加载到GridView与ViewPager组件中。

0 0
原创粉丝点击