Scott Main | 153f8fe | 2012-04-04 17:45:24 -0700 | [diff] [blame] | 1 | page.title=Displaying Bitmaps Efficiently |
Scott Main | 7d711b1 | 2013-03-11 13:52:35 -0700 | [diff] [blame] | 2 | page.tags="bitmaps","images","graphics" |
Scott Main | 153f8fe | 2012-04-04 17:45:24 -0700 | [diff] [blame] | 3 | |
| 4 | trainingnavtop=true |
| 5 | startpage=true |
Scott Main | 153f8fe | 2012-04-04 17:45:24 -0700 | [diff] [blame] | 6 | |
| 7 | @jd:body |
| 8 | |
| 9 | <div id="tb-wrapper"> |
| 10 | <div id="tb"> |
| 11 | |
| 12 | <h2>Dependencies and prerequisites</h2> |
| 13 | <ul> |
| 14 | <li>Android 2.1 (API Level 7) or higher</li> |
Scott Main | 4e2c9dc | 2013-07-23 19:35:17 -0700 | [diff] [blame] | 15 | <li><a href="{@docRoot}tools/support-library/index.html">Support Library</a></li> |
Scott Main | 153f8fe | 2012-04-04 17:45:24 -0700 | [diff] [blame] | 16 | </ul> |
| 17 | |
| 18 | <h2>Try it out</h2> |
| 19 | |
| 20 | <div class="download-box"> |
| 21 | <a href="{@docRoot}shareables/training/BitmapFun.zip" class="button">Download the sample</a> |
| 22 | <p class="filename">BitmapFun.zip</p> |
| 23 | </div> |
| 24 | |
| 25 | </div> |
| 26 | </div> |
| 27 | |
Scott Main | 64fedb7 | 2013-11-12 09:12:38 -0800 | [diff] [blame] | 28 | <a class="notice-developers-video wide" href="http://www.youtube.com/watch?v=rsQet4nBVi8"> |
| 29 | <div> |
| 30 | <h3>Video</h3> |
| 31 | <p>DevBytes: Bitmap Allocation</p> |
| 32 | </div> |
| 33 | </a> |
| 34 | |
| 35 | <a class="notice-developers-video wide" href="http://www.youtube.com/watch?v=pMRnGDR6Cu0"> |
| 36 | <div> |
| 37 | <h3>Video</h3> |
| 38 | <p>DevBytes: Making Apps Beautiful - Part 4 - Performance Tuning</p> |
| 39 | </div> |
| 40 | </a> |
| 41 | |
kmccormick | 7571542 | 2013-03-04 15:25:40 -0800 | [diff] [blame] | 42 | <p>Learn how to use common techniques to process and load {@link |
Scott Main | 153f8fe | 2012-04-04 17:45:24 -0700 | [diff] [blame] | 43 | android.graphics.Bitmap} objects in a way that keeps your user interface (UI) components responsive |
| 44 | and avoids exceeding your application memory limit. If you're not careful, bitmaps can quickly |
| 45 | consume your available memory budget leading to an application crash due to the dreaded |
| 46 | exception:<br />{@code java.lang.OutofMemoryError: bitmap size exceeds VM budget}.</p> |
| 47 | |
| 48 | <p>There are a number of reasons why loading bitmaps in your Android application is tricky:</p> |
| 49 | |
| 50 | <ul> |
| 51 | <li>Mobile devices typically have constrained system resources. Android devices can have as little |
| 52 | as 16MB of memory available to a single application. The <a |
| 53 | href="http://source.android.com/compatibility/downloads.html">Android Compatibility Definition |
| 54 | Document</a> (CDD), <i>Section 3.7. Virtual Machine Compatibility</i> gives the required minimum |
| 55 | application memory for various screen sizes and densities. Applications should be optimized to |
| 56 | perform under this minimum memory limit. However, keep in mind many devices are configured with |
| 57 | higher limits.</li> |
| 58 | <li>Bitmaps take up a lot of memory, especially for rich images like photographs. For example, the |
Adam Koch | 9977ddd | 2012-08-14 14:53:42 -0400 | [diff] [blame] | 59 | camera on the <a href="http://www.android.com/devices/detail/galaxy-nexus">Galaxy Nexus</a> takes |
| 60 | photos up to 2592x1936 pixels (5 megapixels). If the bitmap configuration used is {@link |
Scott Main | 153f8fe | 2012-04-04 17:45:24 -0700 | [diff] [blame] | 61 | android.graphics.Bitmap.Config ARGB_8888} (the default from the Android 2.3 onward) then loading |
| 62 | this image into memory takes about 19MB of memory (2592*1936*4 bytes), immediately exhausting the |
| 63 | per-app limit on some devices.</li> |
| 64 | <li>Android app UI’s frequently require several bitmaps to be loaded at once. Components such as |
| 65 | {@link android.widget.ListView}, {@link android.widget.GridView} and {@link |
| 66 | android.support.v4.view.ViewPager} commonly include multiple bitmaps on-screen at once with many |
| 67 | more potentially off-screen ready to show at the flick of a finger.</li> |
| 68 | </ul> |
| 69 | |
| 70 | <h2>Lessons</h2> |
| 71 | |
| 72 | <dl> |
| 73 | <dt><b><a href="load-bitmap.html">Loading Large Bitmaps Efficiently</a></b></dt> |
| 74 | <dd>This lesson walks you through decoding large bitmaps without exceeding the per application |
| 75 | memory limit.</dd> |
| 76 | |
| 77 | <dt><b><a href="process-bitmap.html">Processing Bitmaps Off the UI Thread</a></b></dt> |
| 78 | <dd>Bitmap processing (resizing, downloading from a remote source, etc.) should never take place |
| 79 | on the main UI thread. This lesson walks you through processing bitmaps in a background thread |
| 80 | using {@link android.os.AsyncTask} and explains how to handle concurrency issues.</dd> |
| 81 | |
| 82 | <dt><b><a href="cache-bitmap.html">Caching Bitmaps</a></b></dt> |
| 83 | <dd>This lesson walks you through using a memory and disk bitmap cache to improve the |
| 84 | responsiveness and fluidity of your UI when loading multiple bitmaps.</dd> |
| 85 | |
kmccormick | 7571542 | 2013-03-04 15:25:40 -0800 | [diff] [blame] | 86 | <dt><b><a href="manage-memory.html">Managing Bitmap Memory</a></b></dt> |
| 87 | <dd>This lesson explains how to manage bitmap memory to maximize your app's performance.</dd> |
| 88 | |
Scott Main | 153f8fe | 2012-04-04 17:45:24 -0700 | [diff] [blame] | 89 | <dt><b><a href="display-bitmap.html">Displaying Bitmaps in Your UI</a></b></dt> |
| 90 | <dd>This lesson brings everything together, showing you how to load multiple bitmaps into |
| 91 | components like {@link android.support.v4.view.ViewPager} and {@link android.widget.GridView} |
| 92 | using a background thread and bitmap cache.</dd> |
| 93 | |
Adam Koch | 9977ddd | 2012-08-14 14:53:42 -0400 | [diff] [blame] | 94 | </dl> |