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