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